In C, a few functions are real nasty and scanf() is one of them. It's specially bad for inputting strings.
What will happen if you give a string:
-> scanf("%s", string)
-> Input: "C is stupid".
Well, you will have only "C" in the "string".
Reason: It happens because scanf() considers white-spaces as delimiter. So "C" being followed by a white space is considered as the only input. Simple, isn't it?
Remedy: We have something like follows:
int main()
{
char arr[10];
scanf("%*[ \t\n]%s", arr);
puts(arr);
}
IT DOESN'T WORK!!!
Only way is this:
#define SIZE 128
int main() {
char arr[SIZE];
char ch;
int index = 0;
while(ch != '\n' && index < SIZE)
{
ch = getchar();
arr[index] = ch;
index++;
}
arr[index] = '\0';
}
Done!
No comments:
Post a Comment