填空题 请补充函数proc(),该函数的功能是:删除字符数组中小于指定字符的字符,指定字符从键盘输入,结果仍保存在原数组中。
例如,输入“abcdefghij”,指定字符为“f”,则结果输出“fghij”。
注意:部分源程序给出如下。
请勿改动main()函数和其他函数中的任何内容,仅在函数proc()的横线上填入所编写的若干表达式或语句。
试题程序:
#include<stdlib.h>
#include<stdio.h>
#define M 80
void proc(char str[], char ch)
{
int i=0, j=0;
while(str[i])
{
if(str[i]<ch)
1;
else
{
2;
i++;
}
}
3;
}
void main()
{
char str[N], ch;
system("CLS");
printf("/n Input a string:/n");
gets(str);
printf("/n***original string***/n");
puts(str);
printf("/n Input a charactor:/n");
scanf("%c", &ch);
proc(str, ch);
printf("/n***new string***/n");
puts(str);
}
【正确答案】
【答案解析】i++ str[j++]=str[i] str[j]="/0"[解析] 要删除字符串中小于指定字符的字符,就要把字符串中每一个字符跟指定字符相比较。小于指定字符的字符不予处理,因此第一处填“i++”;把大于等于指定字符的字符保存在原字符串中,因此第二处填“str[j++]=str[i]”;处理完整个字符串后,为新生成的字符串添加结束符,因此第三处填“str[j]="/0"”。