下列给定程序中,函数proc()的功能是:读入一个字符串(长度<20),将该字符串中的所有字符按ASCII码升序排序后输出。
例如,输入opdye.则应输出deopy。
请修改程序中的错误.使它能得到正确结果。
注意:不要改动main()函数.不得增行或删行,也不得更改程序的结构。
试题程序:
#include<string.h>
#include<stdlib.h>
#include<eonio.h>
#include<stdio.h>
//****found****
int proc(char str[])
{
char C;
unsigned i,j;
for(i=0:i<Strlen(str)=1;it+)
for(j=i+1;j-<strlen(str);j++)
if(str[i]>str[j3)
{
c==strEj]:
//****found****
Str[j]=str[i++];
slr[i]==C;
}
)
void main()
(
char str[81];
system(”CLS”)。
printf(”\nPlease enter a character
string:”);
gets(str);
printf(”\n\nBefore sortin9:n%s”,str);
proc(str);
printf(”\nAiler sorting decendingly:\n
%S”,str);
}
参考答案
答案解析
(1)错误:intproc(charstr[])正确:voidproc(charstr[])(2)错误:str[j]==str[i++];正确:strEj]一str[i];
【解析】由主函数中的函数调用可知,函数proc()没有返回值。因此,“intproc(charstr[])”应改为“voidproc(charstr[])”;由函数proc()可知,if语句块完成将字符串str中的第i个元素与第j个元素相交换。因此,“strEj3一str[i++];”应改为“strEj]-str[i];”。