阅读下列程序,当运行函数时,输入asd af aa z67,则输出为 #include #include #include int fun (char *str) { int i,j=0; for(i=0;str[i]!=′ \0′;

admin2010-12-10  20

问题 阅读下列程序,当运行函数时,输入asd af aa z67,则输出为
#include
#include
#include
int fun (char *str)
{ int i,j=0;
  for(i=0;str!=′ \0′;i++)
  if(str!=′ ′)str[j++]=str;
  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从串头到串尾逐一走动,每走到一个字符都判断其是否为空格,若不是空格(注意在if()的单引号之间有一个空格),则将其保存str[j]中。注意j的下标变化、初值及最后加串结束符’\0’。
转载请注明原文地址:https://kaotiyun.com/show/asPp777K
0

最新回复(0)