请补充函数fun() 该函数的功能是;删除字符数组中小于等于指定字符的字符 指定字符从键盘输
问题详情
请补充函数fun(),该函数的功能是;删除字符数组中小于等于指定字符的字符,指定字符从键盘输入,结果仍保存
例如,输入“abcdefghij”,指定字符为‘d’,则结果输出“defghij”。
注意:部分源程序给出如下。
请勿改动主函数main和其他函数中的任何内容,仅在函数fun()的横线上填入所编写的若干表达式或语句。
试题程序:
include <stdio. h>
define N 80
void fun (char s[], char ch)
{
int i=0, j=0;
while (s [i] )
{
if (s Iii<ch)
【 】;
else
{
【 】
i++;
}
}
【 】
}
main ()
{
char str [N], ch;
clrscr ();
printf("n Input a string:n");
gets (str);
printf("n*** original string ***n");
puts (str);
printf("n Input a character:n");
scanf ("%c", &ch);
fun(str, ch);
printf("n*** new string ***n");
puts (str);
}请帮忙给出正确答案和分析,谢谢!
参考答案
正确答案:i++s[j++]=s[i]s[j]='\0'
i++s[j++]=s[i]s[j]='\0' 解析:第一空:如果当前字符比指定字符小,则什么都不做,通过i++访问下一个字符。第二空,如果当前字符大于等于指定字符,则将它保存在字符数组s中。第三空:通过保存比指定字符大或相等的字符,来实现删除比指定字符小的字符。处理结束后,在字符串s最后要加上结束标记符‘\0’。