Description 输入一个字符串str和一个子串s,统计str中子串s的个数。 Input 输入数据有2行,第一行为str,第二行为s,字符串长度不超过128。 Output 输出子串的个数 Sample Input sdf$$$sdf$$ sdf Sample Output 2
#include <stdio.h> #include <string.h> int main() { int i; int j; int l; int n; int m; int count; int flag; char a[129]; char b[129]; gets(a); gets(b); n=strlen(a); m=strlen(b); count=0; for(i=0; i<n; i++) { flag=1; j=0; if(a[i]==b[j]) { l=i; for(j=1; j<m; j++) { if(b[j]!=a[++l]) { flag=0; break; } } if(flag) { count++; } } } printf("%d", count); return 0; }