问答题
给定程序MODI1.C中函数 fun 的功能是: 读入一个字符串(长度<20 ),将该字符串中的所有字符按ASCII码升序排序后输出。
例如, 若输入: edcba, 则应输出: abcde。
请改正程序中的错误,使它能统计出正确的结果。
注意:不要改动 main 函数,不得增行或删行,也不得更改程序的结构!
给定源程序:
#include
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 = t[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 );
}
【正确答案】第一处:外for循环的初始值应是strlen(t)-1。
第二处:由于是按升序排序,所以应 if(t[j]>t[j+1])。
【答案解析】