标签:blog http io os ar for sp 2014 on
找出一个字符串内某个子串的个数。
例如:
子串为:lan
输入:lanzhihui is a good boy lan la lalalan
输出:3
//检索子串的个数 #include<iostream> #include<string> using namespace std; int find_str(string s,const char *str) { if(str==NULL||s.empty()) return 0; int len=s.size(); const char *cp=str; int sum=0; int i=0; bool falg=false; while(i<len) { falg=false; while(s[i]==*str) { i++; str++; falg=true; if(*str=='\0') { sum++; str=cp; } if(i==len) { return sum; } } str=cp; if(!falg) { i++; } } return sum; } int main() { string s; for(;;) { getline(cin,s); int sum=find_str(s,"lan"); cout<<sum<<endl; } system("pause"); return 0; }
标签:blog http io os ar for sp 2014 on
原文地址:http://blog.csdn.net/lanzhihui_10086/article/details/40088647