编程练习4

 1 //查找字符串主串中的子串的个数
 2 #include <iostream>
 3 using namespace std;
 4 
 5 int Findstr(char* str1,char* str2)
 6 {
 7     int count=0;
 8     int len1=strlen(str1);
 9     int len2=strlen(str2);
10     char *str;
11     char tmp[20]={0};
12 
13     for(int i=0;i<len1-len2+1;++i)
14     {    
15         strncpy(tmp,str1+i,len2);
16         if((str=strstr(tmp,str2))!=NULL)
17             count++;
18     }
19     return count;
20 }
21 
22 int main()
23 {
24     char str1[] = {"abcdefgbcdbcd"};
25     char str2[] = {"bcd"};
26     cout<<Findstr(str1, str2)<<endl;
27     
28  return 0;
29 }

 

posted on 2012-09-12 08:46  lyncre  阅读(122)  评论(0)    收藏  举报

导航