二、改錯(cuò)題:給定程序MODI1.C中函數(shù)fun的功能是: 在字符串的最前端加入n個(gè)*號(hào), 形成新串, 并且覆蓋原串。
注意: 字符串的長(zhǎng)度最長(zhǎng)允許為79。
請(qǐng)改正函數(shù)fun中指定部位的錯(cuò)誤, 使它能得出正確的結(jié)果。
注意: 不要改動(dòng)main函數(shù), 不得增行或刪行, 也不得更改程序的結(jié)構(gòu)!
給定源程序:
#include
#include
void fun (char s[], int n)
{
char a[80] , *p;
int i;
/**********found***********/
s=p;
for(i=0; i
do
{a[i]=*p;
i++;
}
/**********found***********/
while(*p++)
a[i]=0;
strcpy(s,a);
}
main()
{int n; char s[80];
printf("\nEnter a string : "); gets(s);
printf("\nThe string \"%s\"\n",s);
printf("\nEnter n (number of *) : "); scanf("%d",&n);
fun(s,n);
printf("\nThe string after insert : \"%s\" \n" ,s);
}
解題答案:
/**********found***********/
p=s;
/**********found***********/
while(*p++);
******************************************