getchar() ?
-
Can someone please explain to me why, when u exceed 20 characters input, it doesnt stop but just keeps on accepting input, and somehow ends up passing through the loop a few times instantaneously? I mean.. just try typing in some 60 characters or so to see what i mean:
#include <stdio.h>
int IsPalindrome(const char*);
int main(void)
{
char str[21]={NULL};
int i=0;
char c=NULL;do
{
if(str[0]!=NULL)
{
if(IsPalindrome(str))
printf("This is a palindrome!\n");
else
printf("This isn't a palindrome.\n");
}
i = 0;
while((c=getchar())!='\n' && i<21)
{
if((c>='a' && c<='z') || c=='$')
{
str[i] = c;
i++;
}
}
str[i] = NULL;
} while(str[0]!='$');return 0;
}int IsPalindrome(const char* str)
{
int len=0, i=0;
// Get string length
while(str[len++]!=NULL) {}
len--;for(i=0;i
Kuniva
-
Can someone please explain to me why, when u exceed 20 characters input, it doesnt stop but just keeps on accepting input, and somehow ends up passing through the loop a few times instantaneously? I mean.. just try typing in some 60 characters or so to see what i mean:
#include <stdio.h>
int IsPalindrome(const char*);
int main(void)
{
char str[21]={NULL};
int i=0;
char c=NULL;do
{
if(str[0]!=NULL)
{
if(IsPalindrome(str))
printf("This is a palindrome!\n");
else
printf("This isn't a palindrome.\n");
}
i = 0;
while((c=getchar())!='\n' && i<21)
{
if((c>='a' && c<='z') || c=='$')
{
str[i] = c;
i++;
}
}
str[i] = NULL;
} while(str[0]!='$');return 0;
}int IsPalindrome(const char* str)
{
int len=0, i=0;
// Get string length
while(str[len++]!=NULL) {}
len--;for(i=0;i
Kuniva
Change the
while
loop to:while((c = getche()) != 0x0d && i<21)
For the
IsPalindrome()
function, why not just callstrrev()
followed bystrcmp()
?
"Opinions are neither right nor wrong. I cannot change your opinion of me. I can, however, change what influences your opinion." - David Crow
-
Can someone please explain to me why, when u exceed 20 characters input, it doesnt stop but just keeps on accepting input, and somehow ends up passing through the loop a few times instantaneously? I mean.. just try typing in some 60 characters or so to see what i mean:
#include <stdio.h>
int IsPalindrome(const char*);
int main(void)
{
char str[21]={NULL};
int i=0;
char c=NULL;do
{
if(str[0]!=NULL)
{
if(IsPalindrome(str))
printf("This is a palindrome!\n");
else
printf("This isn't a palindrome.\n");
}
i = 0;
while((c=getchar())!='\n' && i<21)
{
if((c>='a' && c<='z') || c=='$')
{
str[i] = c;
i++;
}
}
str[i] = NULL;
} while(str[0]!='$');return 0;
}int IsPalindrome(const char* str)
{
int len=0, i=0;
// Get string length
while(str[len++]!=NULL) {}
len--;for(i=0;i
Kuniva
The problem here is that you've allocated a char array of size 21, i.e. with subscripts 0 to 20. Then, you're accepting input of length up to 20 characters, to fill the array you created. This is where the problem occurs, because in actual fact you should only be accepting 19 characters of input, since the twentieth needs to be the NULL string terminator. To clarify, note that str[21] is NOT inside the bounds of the array. Hope this helps, -- Andrew.
-
Change the
while
loop to:while((c = getche()) != 0x0d && i<21)
For the
IsPalindrome()
function, why not just callstrrev()
followed bystrcmp()
?
"Opinions are neither right nor wrong. I cannot change your opinion of me. I can, however, change what influences your opinion." - David Crow
-
The problem here is that you've allocated a char array of size 21, i.e. with subscripts 0 to 20. Then, you're accepting input of length up to 20 characters, to fill the array you created. This is where the problem occurs, because in actual fact you should only be accepting 19 characters of input, since the twentieth needs to be the NULL string terminator. To clarify, note that str[21] is NOT inside the bounds of the array. Hope this helps, -- Andrew.
Thats not what I was asking, it doesn't have anything to do with my question. What does the size of the array have to do with the while loop not really running? If u don't believe me change it and see for urself. What I was asking is why when u enter like 45 characters on the command line and press enter, it'll print three lines of output. It's supposed to simply stop at 20 chars (or 19 if u fix it whatever) and show the output, but it doesnt, it just keeps on accepting input even though i should become larger than 20, its like it doesnt execute the body of the loop at all.. Kuniva --------------------------------------------
-
The problem here is that you've allocated a char array of size 21, i.e. with subscripts 0 to 20. Then, you're accepting input of length up to 20 characters, to fill the array you created. This is where the problem occurs, because in actual fact you should only be accepting 19 characters of input, since the twentieth needs to be the NULL string terminator. To clarify, note that str[21] is NOT inside the bounds of the array. Hope this helps, -- Andrew.
NVM, got it, the getchar() function just behaves differently from what I anticipated. It doesn't just wait for one key to be pressed and then return the result like i thought, instead u can enter as many chars as u want untill u hit return and they all end up in the input buffer, and it returns only the first char u enterred.. kinda weird but whatever. Kuniva --------------------------------------------
-
Because we couldn't use string functions. And I don't see how u explain what happens here.. Kuniva --------------------------------------------
Kuniva wrote: Because we couldn't use string functions. Fair enough. Kuniva wrote: And I don't see how u explain what happens here.. What's to explain? I trust you know that
getchar()
buffers input whilegetche()
does not. That's why I suggested using it.
"Opinions are neither right nor wrong. I cannot change your opinion of me. I can, however, change what influences your opinion." - David Crow
-
Kuniva wrote: Because we couldn't use string functions. Fair enough. Kuniva wrote: And I don't see how u explain what happens here.. What's to explain? I trust you know that
getchar()
buffers input whilegetche()
does not. That's why I suggested using it.
"Opinions are neither right nor wrong. I cannot change your opinion of me. I can, however, change what influences your opinion." - David Crow