在任意输入两个字符串,第二个作为子串,检查第一个字符串中含有几个这样的子串。
C语言实现:
#include “stdio.h"
#include "string.h"
#include "stdlib.h"
void main()
{
char *s1;
char *s2;
int len1,len2,i=0,j=0,count=0;
s1=(char *)malloc(sizeof(char)*100);
s2=(char *)malloc(sizeof(char)*100);
scanf("%s",s1);
scanf("%s",s2);
len1=strlen(s1);
len2=strlen(s2);
while(i<len1)
{
if(s1[i]==s2[j])
{
i++;
j++;
}
else
{
i=i-j+1;
j=0;
}
if(j==len2)
{ count++;
j=0;
}
}
printf("%d",count);
}
??