题目:计算一个字符串中的子串个数
程序如下:
#include<stdio.h>
int main()
{
char str1[50];
printf("please input str1:\n");
scanf("%s",str1);
char str2[10];
printf("please input str2:\n");
scanf("%s",str2);
char *p=str1;
char *q=str2;
int sum=0;
while(*p!='\0')
{
if(*p==*q)
{
while(*q!='\0')
{
p++;
q++;
}
sum++;
}
else
{
p++;
q++;
}
{
char str1[50];
printf("please input str1:\n");
scanf("%s",str1);
char str2[10];
printf("please input str2:\n");
scanf("%s",str2);
char *p=str1;
char *q=str2;
int sum=0;
while(*p!='\0')
{
if(*p==*q)
{
while(*q!='\0')
{
p++;
q++;
}
sum++;
}
else
{
p++;
q++;
}
}
return 0;
}
}
然而当我执行时输入第二个字符串时它就自动跳出来了,找了半天也没找出原因。