问题
单项选择题
阅读下列程序,当运行程序时,输入asd af aa z67,则输出为( )。 #include <sldio.h> int fun (char *str) { int i,j=0;for(i=0;str[i]! =’\0’;i++) if(str[i]! =") str[j++]=str[i];str[j]=’\0’; } main() { char str[81];int n;printf("Input a string:");gets(str);fun(str);printf("%s\n",str); }
A.asdafaaz67
B.asd af aa z67
C.asd
D.z67
答案
参考答案:A
解析: 本题题意是删除字符串中所有空格。由于C语言中没有直接删除字符的操作,所以删除字符的操作都是采用“留下”字符的算法来实现。从串头str[0]到串尾逐一比较,判断其是否为空格,若不是空格,则将其保存在str[j]中,最后加串结束符“\0”。