The following code works with scanf, but I don't know how to make it work with fgets (the search always fails), could you help me please (I'm a beginner in C) ?
And if possible, could you tell me why it doesn't seem to work with fgets?
#include <stdio.h>
#include <string.h>
char tracks[][80] = {
"I left my heart in Harvard Med School",
"Newark, Newark - a wonderful town",
"Dancing with a Dork",
"From here to maternity",
"The girl from Iwo Jima",
};
int main(){
char word[80];
puts("Hello, type a word: ");
fgets(word, 80, stdin);
// scanf("%79s", word);
for( int i= 0; i < 5; i++){
if( strstr(tracks[i], word) ){
printf("Found the track: %s\n", tracks[i]);
break;
}
}
return 0;
}
fgetsleaves the newline in the string,scanfdoesn't.