#include <stdio.h>
#include <string.h>
int time(char *s1,char*s2);
int main()
{
char str[256];
char son[25];
int t;
printf("Please input a long strings:");
scanf("%s",str);
printf("Please input a son strings:");
scanf("%s",son);
t = time(str,son);
printf("The son strings total show %d times in long strings.\n",t);
return 0;
}
int time(char *s1,char*s2)
{
char *p1 = s1,*p2 = s2;
int count = 0,k,len;
len = strlen(s2);
while( (*p1) != '\0' )
{
k = 0;
while( (*p1) == (*p2) && (*p2) != '\0' ) //查找字符串中的子串
{
p1++;
p2++;
k++;
}
if( k == len )
count++;
p2 = s2;
p1++;
}
return count;
}
程序运行示例:
Please input a long strings:zxcvbnzxcvzxcvbnbvczxcvzxcvbn
Please input a son strings:zxc
The son strings total show 5 times in long strings.