C語言學習006:歌曲搜索,語言學習006
1 #include <stdio.h>
2 #include <string.h> //字符串處理庫
3
4 char tracks[][80]={
5 "I left my heart in Harvard Med School",
6 "Newwark,Newwark - a wonderful town",
7 "From here to maternity",
8 "The girl from Iwo Jima",
9 };
10
11 void find_track(char search_for[]){
12 int i;
13 for(i=0;i<5;i++){
14 if(strstr(tracks[i],search_for))//strstr字符串查找,如果找到返回字符串在存儲器中的位置,如果沒有找到返回0
15 printf("Track %i: '%s'\n",i,tracks[i]);
16 }
17 }
18
19 int main(){
20 char search_for[80];
21 printf("Search for:");
22 fgets(search_for,80,stdin);
23 search_for[strlen(search_for)-1]='\0';//strlen返回字符串的長度
24 find_track(search_for);
25 return 0;
26 }