|
发表于 2019-12-1 19:09:03
|
显示全部楼层
本楼为最佳答案
本帖最后由 bin554385863 于 2019-12-1 19:10 编辑
- #include <iostream>
- #include <string>
- using std::cout;
- using std::endl;
- using std::string;
- int func(string &str, string &s) //str为母串.s为子串
- {
- int count = 0, inx = 0, t = 0;
- while (t != -1)
- {
- t = str.find(s, inx);
- inx = t + s.size();
- count++;
- }
- return count - 1;
- }
- int main(int argc, char const *argv[])
- {
- string str = "hello hello C++ C++ java java java ffd dsasdf";
- string s = "java";
- string a = "hello";
- string b = "ffd";
- cout<<"java = " <<func(str , s)<<"\nhello = "<<func(str, a)<<"\nffd = "<<func(str, b);
- return 0;
- }
复制代码
================================================
Microsoft Windows [版本 10.0.18363.476]
(c) 2019 Microsoft Corporation。保留所有权利。
E:\Users\admin\Documents\VScode\Code>c:\Users\admin\.vscode\extensions\ms-vscode.cpptools-0.26.1\debugAdapters\bin\WindowsDebugLauncher.exe --stdin=Microsoft-MIEngine-In-1npb2ofw.ojm --stdout=Microsoft-MIEngine-Out-owzfxyrt.2uf --stderr=Microsoft-MIEngine-Error-jm4ywmqx.v4c --pid=Microsoft-MIEngine-Pid-1wxotdp3.b3l --dbgExe=D:\MinGW\bin\gdb.exe --interpreter=mi
java = 3
hello = 2
ffd = 1
E:\Users\admin\Documents\VScode\Code> |
|