改错题
给定程序MODI1.c中函数fun的功能:将s所指字符串的正序和反序进行连接,形成一个新串放在t所指的数组中。
例如,当s所指字符串为“ABCD”时,则t所指字符串中的内容应为“ABCDDCBA”。
请改正程序中的错误,使它能得出正确的结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构!
给定源程序如下:
#include <stdio.h>
#include <string.h>
/************found************/
void fun (char s, char t)
{
int i, d;
d = strlen(s);
for (i = 0; i<d; i++) t[i] = s[i];
for (i = 0; i<d; i++) t[d+i] = s[d-1-i];
/************found************/
t[2*d-1] = '\0';
}
main()
{
char s[100], t[100];
printf('\nPlease enter string S:'); scanf('%s', s);
fun(s, t);
printf('\nThe result is: %s\n', t);
}
【正确答案】(1)void fun(char*s,char*t) (2)t[2*d]='\0';或t[d+i]='\0';或t[2*d]=0;或t[d+i]=0; 答案考生文件夹
【答案解析】(1)由主函数的调用方式可知,fun()函数参数为字符数组,故fun()函数的形参要写成指针形式。 (2)程序中把最后一个字符赋值为'\0',是错误的。按题意,应在最后一个字符后面加上'\0'。