(i=strlen(t)-1为什么要建1
第12课 改错题真题
给定程序MODI1.C中函数 fun 的功能是: 读入一个字符串(长度<20 ),将该字符串中的所有字符按ASCII码升序排序后输出。
例如, 若输入: edcba, 则应输出: abcde。
请改正程序中的错误,使它能统计出正确的结果。
注意:不要改动 main 函数,不得增行或删行,也不得更改程序的结构!
#include <stdio.h>
#include <string.h>
void fun( char t[] )
{
char c;
int i, j;
/**********found***********/
for( i = strlen( t )-1; 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\"\n", s );
}