二、改錯題:給定程序MODI1.C中函數(shù)fun的功能是:從s所指字符串中,找出與t所指字符串相同的子串的個數(shù)作為函數(shù)值返回。例如,當(dāng)s所指字符串中的內(nèi)容為:"abcdabfab",t所指字符串的內(nèi)容為:
"ab",則函數(shù)返回整數(shù)3。請改正程序中的錯誤,使它能得出正確的結(jié)果。
注意:不要改動main 函數(shù),不得增行或刪行,也不得更改程序的結(jié)構(gòu)!
給定源程序:
#include
#include
int fun (char *s, char *t)
{
int n; char *p , *r;
n = 0;
while (*s)
{p = s; r = t;
while (*r)
if (*r == *p) {
/************found************/
r++; p++
}
else break;
/************found************/
if (r == '\0')
n++;
s++;
}
return n;
}
main()
{
char s[100], t[100]; int m;
printf("\nPlease enter string S:"); scanf("%s", s);
printf("\nPlease enter substring t:"); scanf("%s", t);
m = fun(s, t);
printf("\nThe result is: m = %d\n", m);
}
解題答案:
/************found************/
r++; p++;
/************found************/
if(*r=='\0')
******************************************