c - fgets doesn't work after scanf -
#include <stdio.h> #include <string.h> #include <ctype.h> void delspace(char *str); int main() { int i, loops; char s1[101], s2[101]; scanf("%d", &loops); while (loops--) { fgets(s1, 101, stdin); fgets(s2, 101, stdin); s1[strlen(s1)] = '\0'; s2[strlen(s2)] = '\0'; if (s1[0] == '\n' && s2[0] == '\n') { printf("yes\n"); continue; } delspace(s1); delspace(s2); (i = 0; s1[i] != '\0'; i++) s1[i] = tolower(s1[i]); (i = 0; s2[i] != '\0'; i++) s2[i] = tolower(s2[i]); if (strcmp(s1, s2) == 0) { printf("yes\n"); } else { printf("no\n"); } } return 0; } void delspace(char* str) { int = 0; int j = 0; char stmp[strlen(str)]; while (str[i++] != '\0') { if (str[i] != ' ') { stmp[j++] = str[i]; } } stmp[j] = '\0'; strcpy(str, stmp); }
after entered "loops", "s1" assigned blank line automatically. how happen? i'm sure keyboard works fine.
scanf()
reads asked to, leaving following \n
end of line in buffer fgets()
read it. either consume newline, or (my preferred solution) fgets()
, sscanf()
string.
Comments
Post a Comment