笔试题目!~~~
写出在母串中查找子串出现次数的代码 ?高手出个主意咯
[解决办法]
//手写了一个,你自己测试一下
char *mom; //母串
char *cld; //子串
int cnt = 0; //记数器
char *p = strstr(mom,cld);
while(p!=NULL)
{
cnt++;
p = strstr(p,cld);
}
printf( "%d\n ",cnt);
[解决办法]
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
char str1[255],str2[255],*p1,*p2, *temp;
int sum=0;
cout < < "intput two strings " < <endl;
cin> > str1;
cin> > str2;
p1=str1;
p2=str2;
while (*p1!= '\0 ')
{
temp = p1;
if(*temp==*p2)
{
while((*temp==*p2)&&(*p2!= '\0 ')&&(*temp!= '\0 '))
{
temp++;
p2++;
}
}
p1++;
if(*p2== '\0 ') sum=sum+1;
p2=str2;
}
cout < <sum;
return 0;
}