0

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");

        }
    }

}
Lavonen
  • 606
  • 1
  • 10
  • 21

1 Answers1

1

I guess my first solution didn't really work out because a white space simply isn't a character (?)

Of course whitespace is a character - in fact, a group of characters are considered whitespace. However, whitespace characters play special role when it comes to scanf: it serves to separate inputs parsed with the format string, except a few special cases.

That is why you are not going to get "abc " from scanf with %s: the trailing whitespace would always be ignored, so neither of your two approaches would work.

This would work:

char buf[100], ch;
scanf("%99s%c", buf, &ch);
if (strcmp(buf, "abc") == 0 && ch == ' ') {
    printf("Yes!\n");
}

The idea is to read the character immediately after %s's capture into a variable, and compare that variable to space character ' '.

Note the use of %99s to limit the size of the input to the allocated length of the buffer (plus an additional character for the null terminator).

Demo.

Important: It goes without saying that you need to check the return value of scanf to see that there was some input for all format specifiers. In the case above you need to check that scanf's return value was 2.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • 1
    You should check the return code from scanf; if there is no whitespace in the input, you will be comparing an uninitialised value (or two, if input is at EOF). – rici Nov 05 '18 at 18:56
  • Thank you a lot! :) I actually managed to isolate the problem even more and it kind of work when I replaced doubble quotes with single quotes ('abc') but in the end I didn't manage to transfer the solution into this specific function. I guess that was luck though because I had no idea about what I was doing... – Lavonen Nov 05 '18 at 18:59
  • @rici Right, I added a note at the bottom to mention this. Thank you! – Sergey Kalinichenko Nov 05 '18 at 19:00
  • @Lavonen Using single quotes around multiple characters, as in `'abc'` [creates a multicharacter literal](https://stackoverflow.com/q/3960954/335858), which is something you seldom want. – Sergey Kalinichenko Nov 05 '18 at 19:02