/*输入一个字符串,计算字符串中字串中子串出现的次数*/
#include <stdio.h>
#include <string.h>
int fun(char *src,char *b)
{
int count = 0;
char *ptr = src;
char *tt = b;
int k = 0;
int len = strlen(b);
while(*ptr != '\0')
{
while(*ptr == *tt&&*tt != '\0')
{
ptr++;
tt++;
k++;
if(k == len)
{
count++;
k = 0;
ptr--;
}
}
tt = b;
ptr++;
}
return count;
}
int main()
{
char src[100];
char b[50];
int count;
printf("please input string1:\n");
scanf("%s",src);
printf("please input string2:\n");
scanf("%s",b);
count = fun(src,b);
printf("the result:%d\n",count);
return 0;
}
#include <stdio.h>
#include <string.h>
int fun(char *src,char *b)
{
int count = 0;
char *ptr = src;
char *tt = b;
int k = 0;
int len = strlen(b);
while(*ptr != '\0')
{
while(*ptr == *tt&&*tt != '\0')
{
ptr++;
tt++;
k++;
if(k == len)
{
count++;
k = 0;
ptr--;
}
}
tt = b;
ptr++;
}
return count;
}
int main()
{
char src[100];
char b[50];
int count;
printf("please input string1:\n");
scanf("%s",src);
printf("please input string2:\n");
scanf("%s",b);
count = fun(src,b);
printf("the result:%d\n",count);
return 0;
}