#include<iostream>
using namespace std;
int subString(char *s,char *t)
{
int count = 0;
bool flag = false;
char *temp = NULL;
while(*s != '\0')
{
temp=t;
while(*temp != '\0')
{
if(*temp == *s)
{
s++;
temp++;
flag = true;
}
else
{
s++;
flag = false;
break;
}
}
if(*temp=='\0' && flag == true)
count++;
}
return count;
}
int main()
{
char s[50],sub[8];
cout<<"please input a string:";
cin>>s;
cout<<"please input a substring:";
cin>>sub;
cout<<subString(s,sub)<<endl;
}
计算字符串中子串出现的次数问题:
程序计算结果:

本文介绍了一个使用C++实现的简单程序,该程序能够计算一个指定子串在一个字符串中出现的总次数。用户首先输入主字符串,然后输入要查找的子串,程序将输出子串在主字符串中出现的次数。
2366

被折叠的 条评论
为什么被折叠?



