1. 程式人生 > >字串處理系列:輸入任何一串字串,計算其中ABC子字串的個數

字串處理系列:輸入任何一串字串,計算其中ABC子字串的個數

#include<cstring>
#include<iostream>
using namespace std;
int coutABC(char *s)
{
char *sub = "ABC";
char *teststr=s;
int cnt = 0;
char *p = teststr;
while (*p)
{
if (strncmp(p, sub, 3) == 0)
{
p += 3;
cnt++;
}
else
p++;
}
return cnt;
}
int main()
{
char checkstr[100];
cout << "請輸入字串" << endl;
cin.getline(checkstr, 80);
cout << "ABC的個數: " << coutABC(checkstr) << endl;
return 0;
}