注意,find找到后不光要sum++,还要i=pos,否则会重复查找
continue用来跳过i++
#include<iostream> #include<string> using namespace std; int main(){ string a,b; while(cin>>a>>b){ int len = a.size(); int sum=0; for(int i=0;i<len;i++){ int pos = a.find(b,i); if(pos!=-1){ sum++; i=pos; continue; } } cout<<sum<<endl; } return 0; }