填空题
下列给定程序中,函数fun()的功能是:从字符串s中,删除所有小写字母'c'。
请改正程序中的错误,使它能得出正确的结果。
注意:不要改动main 函数,不得增行或删行,也不得更改程序的结构.
试题程序:
# include <stdio.h>
void fun(char *s)
{int i, j;
for(i=j=0; s[i] != '/0';i++)
if(sill != 'c')
/********************************/
s [j] =s [i];
/**************found*************/
s[i]='/0';
}
main ()
{ char s[80];
printf ("/nEnter a string: ");
gets (s);
printf("The original string: ")
puts (s);
fun (s);
printf("The string after deleted: ");
puts (s); printf ("/n/n "):
}
【正确答案】
1、错误:s[j]=s[i]; 正确:s[j++]=s[i];
【答案解析】(2)错误:s[i]=‘/0’; 正确:s[j]=‘/0’;
[解析] 由本题循环条件if(s[i]!=‘c’),可知,如果字符串中的字符不是‘c’,则执行循环,所以应该是s[j++])=s[i],这样错