问题
单项选择题
阅读下列程序,当运行函数时,输入asd af aa z67,则输出为 #include <stdio.h> #include <ctype.h> #inelude <string.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); puts(str); fun(str); printf("%s\n",str); }
A.asdafaaz67
B.asd af aa z67
C.asd
D.z67
答案
参考答案:A
解析: 本题题意要求删除所有空格,即除了空格以外的其他所有字符都要留下。由于C语言中没有直接删除字符的操作,所以我们对于删除字符的操作都是采用“留下”字符的算法,以前的题目亦是如此。用str[i]从串头到串尾逐一走动,每走到一个字符都判断其是否为空格,若不是空格(注意在if()的单引号之间有一个空格),则将其保存str[j]中。注意 j的下标变化、初值及最后加串结束符“\0”。