C语言学习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<4;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 }

posted @ 2016-04-03 10:11  高效养猪倌  阅读(560)  评论(0编辑  收藏  举报