题解 | #统计字符串中子串出现的次数#
统计字符串中子串出现的次数
https://www.nowcoder.com/practice/9eb684f845a446f3b121472de2ea75cd
#include <iostream> #include <string> using namespace std; int main() { char str[100]; char substr[100]; cin >> str >> substr ; // by find to find the same substring string str1(str); string str2(substr); int postion = 0; int count = 0; while (str1.find(str2, postion) != string::npos) { ++count; postion = str1.find(str2, postion) + 1; } cout << count << endl; return 0; }使用find()函数