c语言:编写一个程序,判断某字串在主串中出现的次数

来源:百度知道 编辑:UC知道 时间:2024/06/16 19:49:00
编写一个程序,判断某字串在主串中出现的次数,字串主串均从键盘输入。(如:主串“asdas as d as”,字串为“as”,则出现4次。)
高手来看看啦,我想要简洁又正确的答案

运行通过

int main()
{
char *ps,*pt,*s="asdas as d as",*t="as";
int num=0;
ps=s;
pt=t;
while(*ps!='\0')
{
while(*pt!='\0')
{

if(*ps==*pt)
{
ps++;
pt++;
}
else
{
s++;
ps=s;
pt=t;
}

if(*pt=='\0')
{
num++;
s++;
ps=s;
pt=t;
}
if(*ps=='\0')
{
break;
}
}
}
cout<<num<<endl;
return 1;
}