字符串找子串个数(发现一个好用的库函数)

#include<bits/stdc++.h>
using namespace std;
int main(void)
{ int n; string s; string p = "wang"; while(cin >> s) { int cnt = 0; size_t found = s.find(p, 0); //若没有找到则返回 string::npos,找到了则返回第一次出现p的位置 while(found != string::npos) { ++cnt; found += 3; if(found >= s.size()) break; found = s.find(p, found); } cout << cnt << endl; } return 0; }

 #include<bits/stdc++.h>头文件包括常用的所有库,用一次就爱上

posted @ 2020-08-20 19:35  victorywr  阅读(560)  评论(0编辑  收藏  举报