填空题
1. 下列给定程序中,函数fun的功能是:先将字符串s中的字符按正序存放到字符串t中,然后把s中的字符按逆序连接到字符串t的后面。
例如,当s中的字符串为“ABCDE”时,则t中的字符串应为“ABCDEEDCBA”。
请改正程序中的错误,使它能得出正确的结果。
注意:不要改动main函数.不得增行或删行,也不得更改程序的结构!
试题程序:
#include<stdlib.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
void fun (char *s, char *t )
{
int i,s1;
s1 =strlen(s);
/*********found*********/
for (i=0;i<=s1; i++)
t[i]=s[i];
for (i=0; i<s1; i++)
t[s1+i]=s[i];
/*********found*********/
t[s1]='\0';
}
void main()
{
char s[100], t[100];
system("CLS");
printf("\nPlease enter string s:");
scanf("%s",s);
fun(s,t);
printf("The result is:% s\n",t);
}
【正确答案】
1、(1)for (i=0; i<sl;i++)
(2)t[2*s1]='\0';
【答案解析】[考点]
本题考查:for循环语句;字符串结束标识‘\0’。
(1)字符串长度为sl,但数组下标从0~sl-1,因此不包括sl。
(2)正序和逆序字符串都加入了t串中,此时t串中最后一个元素的下标为2*sl-1,所以在2*sl下标处加入字符串结束标识‘\0’。