这个程序没有提供任何输出...... 如果输入匹配的字符串,函数find_track应该返回轨道。
#include<stdio.h>
#include<string.h>
char tracks[][80] = {
"I left my heart in Harward Med School",
"Newark, Newark - a wonderful town",
"Dancing with a Dork",
"From here to maternity",
"The girl from Iwo Jima",
};
void find_track(char search_for[])
{
int i;
for(i = 0; i< 5 ; i++)
{
if(strstr(tracks[i],search_for))
{
printf("Track %i: '%s'\n",i,tracks[i]);
}
}
}
int main()
{
char search_for[80];
printf("Search for : ");
fgets(search_for,80,stdin);
find_track(search_for);
return 0;
}
答案 0 :(得分:5)
fgets()读取您输入的行,正如文档所说,这包括您输入到程序的任何换行符。所以你输入“with”并按Enter键,程序将得到字符串“with \ n”。
所以摆脱那个换行符:
if (fgets(search_for,80,stdin) && search_for[0]) [
search_for[strlen(search_for) - 1] = 0;
find_track(search_for);
}