我想搜索是否有任何曲目可用的曲目數組。當我總是給出任何輸入strstr()
函數返回false。如果我使用else塊,則每次其他塊都正在執行。。當我通過任何輸入總是功能返回false
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",
};
// function to search the track with entered search string //
void find_track(char search_for[]) {
int i;
for (i = 0; i < 5; i++) {
/** this if condition is returning false every time */
if (strstr(tracks[i], search_for)){
printf("Track %i: '%s'\n", i, tracks[i]);
}/**else{
printf("some thing went wrong @ %i\n", i);
}**/
}
}
int main() {
char search_for[80];
printf("Search for: ");
fgets(search_for, 80, stdin);
find_track(search_for);
return 0;
}
輸入:
gcc test.c -o test && test
搜索:with
預期的結果:Track 2:'Dancing with a Dork'
請幫助我。
單步與您的調試器。 – KevinDTimm
[從fgets中刪除換行符](https://stackoverflow.com/questions/2693776)。 – user3386109