搜索字符串

题目源地址

Description

搜索字符串

Input

输入两个字符串a,b(字符串长度不超过1000)

Output

输出在a中出现b的次数(每个结果占一行)

Sample Input

abcdefsdabcbacbbc
abc
aabbaabbaabbaa
abbaa

Sample Output

2
3

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <string.h>
 4 
 5 #define MAX 1010
 6 
 7 int main()
 8 {
 9     char a[MAX],b[MAX];
10     while(gets(a)!=NULL)
11     {
12         gets(b);
13         int sum=0,i=0,j=0;
14         int lena,lenb;
15         lena=strlen(a);
16         lenb=strlen(b);
17         while(i<lena)
18         {
19             if(a[i]==b[j])
20             {
21                 i++;
22                 j++;
23             }
24             else
25             {
26                 i=i-j+1;
27                 j=0;
28             }
29             if(j>=lenb)
30             {
31                 sum++;
32                 i=i-j+1;
33                 j=0;
34             }
35         }
36         printf("%d
",sum);
37     }
38 }
原文地址:https://www.cnblogs.com/twomeng/p/9475944.html