下列给定程序中,函数proe()的功能是:在字符串的最骑端加入m个*号,形成新串,并且覆盖原串。
例如,用户输入字符串abcd(以Enter键结束),然后输入m值为3,则结果为***abcd。
注意:字符串的长度最长允许79。
请修改函数proc()中的错误,使它能得出正确的结果i
注意:不要改动main()函数,不得增行或删行,也不得更改程序的结构。
试题程序:
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<conio.h>
void proc(char str[],int m)
{
char a[80],*P;
int i;
//****found****
str=P;
for(i=0;i<m;i++)a[i]='*';
do
{ a[i]=*P;
//****found****
i++:
}
while(*p);
//****found****
a[i]=0;
strcpy(str,a);
}
void main()
{ int m;char str[80];
system("CLS"):
printf("\nEnter£}string:");gets(str);
printf("\nThe string:%s\n",str);
printf("\nEnter ll(number of*):");
scanf("%d",&.rn);
proc(str,m);
printf("\nThe string after inster:
%s\n",str);
}
参考答案
答案解析
(1)错误:str=P;
正确:p=str;
(2)错误:i++;
正确:i++;p++;
(3)错误:a[i]=0;
正确:a[i]='\0';
【解析】由函数proc()可知,变量P是指向字符串的指针,其初始化应为字符串首地址,因此,“str=P;”应改为“P =str;”;将P指向的字符串中的每一个字符插在m个*之后,并存放在字符串数组a中,因此,“i++;”应改为“i++;++;”;操作完成后要为字符串数组添加结束符,因此“a i]=0;”应改为“a[i]='\0';”。