2013年计算机二级C语言上机题库三十五_第3页

考试站(www.examzz.com)   【考试站:中国教育考试第一门户】   2013年5月13日

  编程题
  请编写一个函数,用来删除字符串中的所有空格。
  例如,输入asd af aa z67,则输出为asdafaaz67。
  注意:部分源程序给出如下。
  请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。
  试题程序:#include <stdio.h>
  #include <ctype.h>
  #include <conio.h>
  void fun(char *str)
  {
  }
  main()
  {
  char str[81];
  char Msg[] = "Input a string:";
  int n;
  FILE *out;
  
  printf(Msg) ; 
  gets(str);
  puts(str);
  fun(str);
  printf("*** str: %s\n", str);
  out=fopen ("out.dat", "w");
  fun(Msg);
  fprintf (out, "%s", Msg);
  fclose (out );
  }
  答案是:
  void fun(char *str)
  {
  int i=0;
  char *p=str;
  while(*p)
  {
  if(*p!=’ ’)
  {
  str[i]=*p;
  i++;
  }
  p++;
  }
  str[I]=’\0’;
  }


首页 1 2 3 尾页

相关文章