#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;
}
计算字符串中子串出现的次数问题:
程序计算结果: