2

As a beginner I've been playing around with some of the functions of the library string.h and have some issues regarding the function strcmp.

I wrote the program comparing two string. If they are equal it returns YES and NO otherwise.

#include <stdio.h>
#include <string.h>

int main() {

    char a[100];
    char b[15] = "hello";

    fgets(a, 100, stdin);

    int compare;

    compare = strcmp(a, b);

    if(compare == 0) {

        printf("YES");
    }
    else {

        printf("NO");
    }

    return 0;
}

After running it even when I input from the keyboard a hello I get a NO. When I add the line printf("%d", compare) it turns out that for any input I get a 1, that is, the stopping character in a is greater than the one in b.

Where is my mistake?

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
asd
  • 1,017
  • 3
  • 15
  • 21

3 Answers3

4

fgets(a, 100, stdin); stores the newline you input into the buffer as well. As such a contains "hello\n". That extra character offsets the comparison.

You can either try to remove the newline by some means1, or compare with strncmp instead.


  1. For instance:

    char *newline = strchr(a, '\n');
    if (newline)
      *newline = '\0';
    
StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458
4

It's not a mistake in scanning as such, but the problem is, fgets() scans and stores the trailing newline into the supplied buffer. You need to get rid of that in case you're comparing the buffer with a string literal which does not contain the terminating newline as part of it.

For stripping the trailing newline, you can use something like

size_t len = strlen(a);
if (len > 0 && a[len-1] == '\n') {
    a[--len] = '\0';
}

See this answer for more reference

Without striping out the newline, strcmp() will not announce the comparison as success.

Otherwise, you can use strncmp() and supply the size of the string literal to keep the comparison limited to the valid input.

Community
  • 1
  • 1
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • 1
    `strncmp()` is no good unless you also check the length of the input string to make sure it is as short as the literal string otherwise (for example) `hello` and `hello goodbye` would compare equal. – JeremyP Mar 13 '17 at 14:49
  • @JeremyP Yes, of course, that is why I mentioned _"limited to the valid input"_ – Sourav Ghosh Mar 13 '17 at 16:37
4

fgets appends the new line character corresponding to the Enter key if there is enough space in the source array.

You should remove the character before comparing the string with another string the following way

a[strcspn( a, "\n" )] = '\0';

For example

#include <stdio.h>
#include <string.h>

int main() {

    char a[100];
    char b[15] = "hello";

    fgets(a, 100, stdin);

    a[strcspn( a, "\n" )] = '\0';

    int compare;

    compare = strcmp(a, b);

    if(compare == 0) {

        printf("YES");
    }
    else {

        printf("NO");
    }

    return 0;
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335