欢迎来到天天文库
浏览记录
ID:59338824
大小:15.50 KB
页数:2页
时间:2020-09-04
《统计一字符串在另一个字符串中出现的次数.doc》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、#include#include#includeintfindStr(char*str,char*substr){intn;char*p,*r;n=0;while(*str){p=str;r=substr;while(*r)if(*r==*p){r++;p++;}else{break;}if(*r==' ')n++;str++;}returnn;}voidReadWrite(){charstr[81],substr[10],ch;intn,len,i=0;FILE*rf,*wf;rf=fopen("in.dat","r");
2、wf=fopen("out.dat","w");while(i<25){fgets(str,81,rf);fgets(substr,10,rf);len=strlen(substr)-1;ch=substr[len];if(ch==''
3、
4、ch==0x1a)substr[len]=0;n=findStr(str,substr);fprintf(wf,"%d",n);i++;}fclose(rf);fclose(wf);}main(){charstr[81],substr[10];intn;printf("输入原字符串:");gets(str);printf("输入子字符串:
5、");gets(substr);puts(str);puts(substr);n=findStr(str,substr);printf("n=%d",n);ReadWrite();}利用递归:intfindStr(constchar*s,constchar*child){char*p=strstr(s,child);returnp?1+strstr_count(p+1,child):0;}循环:intfindStr(char*src,char*cmp){for(intcnt=0,cmplen=strlen(cmp);*src!=0;src++){if(strncmp(src,c
6、mp,cmplen)==0){cnt++;}}returncnt;}
此文档下载收益归作者所有