c语言的问题 tk304改错

来源:百度知道 编辑:UC知道 时间:2024/06/06 12:41:32
gc304.c中的函数fun()的功能是:先将在字符串s中的字符按正序存放到t串中,然后把s中的字符按逆序连接到t串的后面。例如:当s中的字符串为:"abc12"时,则t中的字符串应为:"abc1221cba"。
现在程序中发现有两个错误,错误就出现在那些注释行的附近,请你找出错误并将它们改正过来,要求在调试过程中不允许改变程序结构,更不得增删语句。
#include <stdio.h>
#include <string.h>
void fun (char *s, char *t)
{
int i, st;
st = strlen(s);
/* .............. comment one .............. */
for(i=0; i < st; i++)
t[st] = s[i];
for (i=0; i < st; i++)
t[st+i] = s[st-i-1];
/* .............. comment two .............. */
t[st] = '\0';
}
void main( )
{
char s[100], t[100];
printf("\nPlease enter string s: ");
gets(s);
fun(s, t);
printf("The result is: %s\n", t);
}

#include <stdio.h>
#include <string.h>
void fun (char *s, char *t)
{
int i, st;
st = strlen(s);
/* .............. comment one .............. */
for(i=0; i < st; i++)
t[st] = s[i]; //为什么,不是说先连接么?改为t[i]=s[i];
for (i=0; i < st; i++)
t[st+i] = s[st-i-1];
/* .............. comment two .............. */
t[st] = '\0'; //长度错误啊,应该是t[st+st] = '\0';
}
void main( )
{
char s[100], t[100];
printf("\nPlease enter string s: ");
gets(s);
fun(s, t);
printf("The result is: %s\n", t);
}