填空题
1. 请补充函数proc(),该函数的功能是:把字符串str中的字符按字符的ASCII码升序排列,处理后的字符串仍然保存在原串中,字符串及其长度作为函数参数传入。
例如,如果输入“gfedcba”,则输出为“abcdefg”。
注意:部分源程序如下。
请勿改动main()函数和其他函数中的任何内容,仅在函数proc()的横线上填入所编写的若干表达式或语句。
试题程序: #include<stdio.h>
#include<stdlib.h>
#define N 80
void proc(char str[],int n)
{
int i,j;
char ch;
for(i=0;i<n;i++)
for(j=______;j<n;j++)
if(str[i]>str[j])
{
ch=str[j];
______;
str[i]=ch;
}
}
void main()
{
int i=0,strlen=0;
char str[N];
system("CLS");
printf("\nlnput a string: \n");
gets(str);
while(str[i]!='\0')
{
strlen++;
i++;
}
proc(str, strlen);
printf("\n***display string***\n");
puts(str);
}
【正确答案】
1、i+1
str[j]=str[i]
【答案解析】 题目要求把字符串str中的字符按其ASCII码值升序排列,可以使字符串中的每一个字符与其后的所有字符比较,因此,第一空处填“i+1”;把该字符与ASCII码值小于其本身,而且位置在它后面的字符交换,最后,就可以使ASCII码值小的在前、大的在后,因此,第二空处填“str[j]=str[i]”。