问答题
下列给定程序中,函数proc()的功能是:利用插入排序法对字符串中的字符按从小到大的顺序进行排序。插入法的基本方法是:先对字符串中的头2个元素进行排序,然后把第3个字符插入到前2个字符中,插入后前3个字符依然有序;再把第4个字符插入到前3个字符中,待排序的字符串已在主函数中赋予。
请修改程序中的错误,使它能得出正确的结果。
注意:不要改动main()函数,不得增行或删行,也不得更改程序的结构。
试题程序:
#include<string.h>
#include<stdio.h>
#define M 80
void proc(char *arr)
{ int i, j, n; char ch;
n=strlen(arr);
for(i=1; i<n; i++)
//****found****
{c=arr[i];
j=i-1;
while((j>=0)&&(ch<arr[j]))
{
arr[j+1]=arr[j];
j--;
}
arr[j+1]=ch;
}
}
void main()
{
char a[M]="QWERTYUIOPASDFGHJKLMNBVCXZ";
printf("The original string: %s/n", a);
proc(a);
printf("The string after sorting: %s/n/n", a);
}