//找出长串中有几个短串
#include<stdio.h>
#include<string.h>
/**************方法1******************/
int fun(char *s1,char *s2)
{
char *s,*t;
int count=0;
while(*s1 != '\0')
{
s=s1;t=s2;//一定要设置中间变量
while(*t && *s==*t)
{
s++;t++;
}
if(*t == '\0')
{
count++;
}
s1++; //不论*t到达字符串尾,s1都加一
}
return count;
}
/**************方法2******************/
int fun(char *s1,char *s2)
{
char *s,*t;
int count=0;
while(*s1 != '\0')
{
s=s1;t=s2;//一定要设置中间变量
while(*t)
{
if(*s==*t)
{s++;t++;}
else
{s1++;break;}
}
if(*t == '\0')
{
count++;
s1++;
}
}
return count;
}
int main(void)
{
char str1[80];
char str2[10];
puts("Enter a string:");
gets(str1);
puts("short string :");
gets(str2);
printf("result:%d\n",fun(str1,str2));
return 0;
}