fgets()
does not discard any newline character after reading, so it will be part of str
, which will cause the comparison to fail since string
won't have a newline character. To get around this, you simply need to remove the newline character from str
.
str[strlen(str) - 1] = '\0';
if (strcmp(string, str) == 0)
// ...
Ideally, make sure strlen(str) > 0
first, otherwise you will invoke undefined behaviour.