/*
已知一个字符串,比如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 发布