The fgets
function, as stated here, includes the new line (\n
) character, since it is considered a valid character:
A newline character makes fgets stop reading, but it is considered a valid character by the function and included in the string copied to str.
This means that you are going to check, for example:
"I left my heart in Harvard Med School"
with
"I left\n"
so the match doesn't occur. You could try using gets
or replacing the \n
character with a NUL (\0
):
char search_for[80];
fgets(search_for, 80, stdin);
search_for[strlen(search_for)-1] = '\0';