I have created a very simple program that is supposed to match a string where the first word ("abc") ends with a white space. If I write, for example, "abc defg" I should receive a match because of "abc" and the white space. If I write "abcdefg" I should not because there are no white space.
I guess my first solution didn't really work out because a white space simply isn't a character (?). I therefore created the second solution below with a second condition in the IF-statement, however, it didn't work out neither...
Any ideas how a space in the end of a string can be recognized ?
int main(void)
{
while(1)
{
char str[30];
scanf("%s", str);
char *word = "abc "; <------------ QUESTION HERE
// If two first three letters are "abc" + white space
if(strncmp(str, word, 4) == 0) {
printf("Match\n");
} else {
printf("No match\n");
}
}
}
Second code:
int main(void)
{
while(1)
{
char str[30];
scanf("%s", str);
char *word = "abc";
// If two first three letters are "abc" + white space
if(strncmp(str, word, 3) && (isspace(str[3])) == 0) {
printf("Match\n");
} else {
printf("No match\n");
}
}
}