标签:
1 #include <stdio.h> 2 #include <iostream> 3 using namespace std; 4 #include <string.h> 5 int main() 6 { 7 char s[1024],a[1024]; 8 while(scanf("%s",&s)&&s[0]!=‘#‘&&scanf("%s",&a)) 9 { 10 int count=0; 11 char *p=s; 12 int len=strlen(a); 13 while(p = strstr(p,a)){ 14 count++; 15 p += len; 16 } 17 cout<<count<<endl; 18 } 19 }
find应用:
1 #include<iostream> 2 #include<stdio.h> 3 #include<string> 4 #include<algorithm> 5 using namespace std; 6 int main() 7 { 8 string a,b; 9 int pos,count=0; 10 cin>>a; 11 cin>>b; 12 13 pos=a.find(b);//ccabcbbgabchuhiabc 14 // cout<<pos<<endl; 15 while(pos!=-1) 16 { 17 count++; 18 pos=a.find(b,pos+b.size()); 19 20 } 21 cout<<count<<endl; 22 return 0; 23 }
标签:
原文地址:http://www.cnblogs.com/wangmengmeng/p/4640891.html