函数fun的功能是:读入一个字符串(长度<20),将该字符串中的所有字符按ASCII码升序排序后输出。
例如,若输入:edcba,则应输出:abcde。
请改正程序中的错误,使它能得到正确结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。
[试题源程序]
#include<stdio.h>
void fun(char t[])
Char c;
int i,j;
/**********found***********/
for(i=strlen(t);i;i--)
for(j=0;j<i;j++)
/**********found***********/
if(t[j]<t[j+1])
c=[j];
t[j]=t[j+1];
t[j+1]=c;
main()
char s[81];
printf("\nPlease enter a character string:");
gets(s);
printf("\n\nBefore sorting:n\"%s\"",s);
fun(s);
printf("\nAfter sorting decendingly:\n\"%s\"",s);
参考答案:(1)错误:for(i=strlen(t);i;i--)
正确:for(i=strlen(t)-1;i;i--);
(2)错误:if(t[j]<t[j+1])
正确:if(t[j]>t[j+1])
解析: 错误1:根据题意,有外for循环的初始值应是strlen(t)-1。
错误2:由于是按升序排序,所以应if(t[j]>t[j+1])。