/*
已知一个字符串,比如asderwsde,寻找其中的一个子字符串比如sde的个数,如果没有返回0,有的话返回子字符串的个数。
//*/
#include <iostream>
#include <iomanip>
#include <limits>
using namespace std;
bool matchsub(char* pchar, char* schar, int pos);
int main()
{
char pchar[] = "asderwsde";
char schar[] = "sde";
int cnt = 0;
int pos = 0;
int psz = sizeof(pchar);
int ssz = sizeof(schar);
while(pos < psz - ssz){
while(pchar[pos] != schar[0]){
++pos;
}
if(pos > psz - ssz){
break;
}else{
if(matchsub(pchar, schar, pos)){
++cnt;
++pos;
}else{
++pos;
}
}
}
if(cnt > 0){
cout << cnt <<" substring found!" << endl;
}
return 0;
}
bool matchsub(char* pchar, char* schar, int pos)
{
bool flag = true;
int i = 0;
while(schar[i] != '\0' && pchar[pos+i] != '\0'){
if(pchar[pos+i] != schar[i]){
flag = false;
break;
}
++i;
}
return flag;
}
已知一个字符串,比如asderwsde,寻找其中的一个子字符串比如sde的个数,如果没有返回0,有的话返回子字符串的个数
最新推荐文章于 2021-11-11 01:02:53 发布
本文介绍了一个C++程序示例,该程序用于在一个给定的字符串中查找特定子字符串出现的次数。通过遍历主字符串并与子字符串进行比较,程序能够准确地统计出子字符串在主字符串中出现的频率。
1214

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



