通过课程进入 题 号: 134 统计子字符串出现次数(C++) 语言要求: C++
编写一个函数 int SubStrNum(char * str,char * substr) ,
它的功能是:统计子字符串substr在str中出现的次数。
如 str = “hello world hello you”; substr =“hello”,子串substr在str中出现了2次
则输出:
match times=2
如 str =“hhhhhh”;substr =“hh”;子串substr在str中出现了3次
则输出:
match times=3
#include#include using namespace std; int SubStrNum(char * str,char * substr) { int len1=strlen(str); int len2=strlen(substr); int i,j,num=0; for(i=0;i int flag=1; for(j=0;j if(str[i+j]!=substr[j]){ flag=0; } } if(flag==1){ num++; i+=len2-1; } } cout<<"match times="< char s1[255],s2[255]; gets(s1);gets(s2); SubStrNum(s1,s2); return 0; }



