The code performs a fu-yu search for a song . There is an array in the array and the input is what you need to look for, if the input is done through fgets () the program does not work if everything works through scanf () . The question is, why does this happen with fgets () and scanf () and why do we need an asterisk in the global variable char * tracks []?
Here is the whole code
#include <stdio.h> #include <string.h> Why is a star here, or rather a pointer, because this is a global variable
char *tracks[] = { "Я оставил свое сердце в Гарвардской медицинской школе", "Ньюарк, Ньюарк - город, полный чудес town", "Танец с мужланом", "Отсюда и до роддома", "Девченка с острова Иводзима", }; void find_track(char search_for[]) { int i; for (i = 0; i < 5; i++) { if(strstr(tracks[i], search_for)) printf("Песня номер %i: %s\n", i, tracks[i]); } } int main() { char search_for[80];//фу-ия в стеке и яв-ся printf("Искать: "); here the essence of the question begins and all the code is in principle correct
fgets(search_for, 80, stdin); the line below counts the number of entered characters takes 1 and it turns out that it adds to the last address in the array \ 0
search_for[strlen(search_for)-1] = "\0"; * fgets for some reason does not work and adds a new character in the code with scanf there is no such problem *
printf("fgets() приняла: %s;\nДлинна строки: %lu;", search_for, strlen(search_for)); The code with scanf () works, but the search in this case will be done only one word at a time.
scanf("%79s", search_for); find_track(search_for); return 0; }
search_for[strlen(search_for)-1] = "\0";Did you just ignore his diagnosis? - AnT