M054

02-程序修改题 软件154 范金瑞 1030浏览

所属年份:2010.9;2011.3;2012.3

下列给定程序中,函数fun的功能是:先将字符串s中的字符按正序存放到字符串t中,然后把s中的字符按逆序连接到字符串t的后面。
例如,当s中的字符串为”ABCDE”时,则t中的字符串应为”ABCDEEDCBA”。

请改正程序中的错误,使它能得出正确的结果。
注意:部分源程序在文件MODI1.C中,不得增行或删行,也不得更改程序的结构!

#include <stdio.h>
#include <string.h>

void fun (char  *s, char  *t)
{
    int i, sl;
    sl = strlen(s);
    /************found************/
    for( i=0; i<=sl; i++)
        t[i] = s[i];
    for (i=0; i<sl; i++)
        t[sl+i] = s[sl-i-1];
    /************found************/
    t[sl] = '\0';
}

main()
{
    char s[100], t[100];
    printf("\nPlease enter string s:");
    scanf("%s", s);
    fun(s, t);
    printf("The result is: %s\n", t);
}

【参考答案】

(1)for ( i=0; i

【考点分析】
本题考查:for循环语句;字符串结束标识'\0'。

【解题思路】
(1)字符串长度为sl,但数组下标从0到sl-1,因此不包括sl。
(2)正序和逆序字符串都加入了t串中,此时t串中最后一个元素的下标为2*s1-1,所以在2*s1下标处加入字符串结束标识'\0'。