Sunday, 18 August 2013

gets() seems to store some garbage string

gets() seems to store some garbage string

I wish to enter strings with spaces. I used gets(), already taking care of
the newline character [using while( (c = getchar()) != '\n' && c != '\0');
] that may create problem. But the first gets() inputs some stray
characters! Also, if i use scanf( "%[^\n]s", a) instead of gets() some
random string is stored ! can someone help me find out why?
here's the code :
printf(" \n Enter the string");
while( (c =getchar()) != '\n' && c != '\0');
gets(a); // some garbage string is stored in a....if i replace it with
scanf()...then also garbage string is stored
printf(" \n The ENTERED string is %s", a);
printf("\n Enter the substring to be extracted (max 20) ");
while( (c =getchar()) != '\n' && c != '\0');
gets(sub);

No comments:

Post a Comment