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

考试站(www.examzz.com)   【考试站:中国教育考试第一门户】   2013年5月22日
 编程题
  假定输入的字符串中只包含字母和*号。请编写函数FUN,它的功能是:使字符串中前导*号全部移到字符串的尾部。
  例如,若字符串中的内容为*******A*BC*DEF*G****,移动后,字符串中的内容应当是A*BC*DEF*G***********。在编写函数时,不得使用C语言提供的字符串函数。
  注意:部分源程序给出如下。
  请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。
  试题程序:来自www.examzz.com
  #include<stdio.h>
  #include<conio.h>
    voidfun(char*a)
  {
    }
    main() 
  {
  char s[81],*p;
  FILE*out;
  chartest[4][80]={"*******A*BC*DEF*G****","A******B*CD**EF*G*",
  "****A****G*","*d**b**a**e*"};
  inti;
  printf("Enterastring:\n");
  gets(s);
  fun(s);
  printf("Thestringaftermoveing:\n");
  puts(s);
  out=fopen("out.dat","w");
  for(i=0;i<4;i++)
  {
  fun(test[i]);
  fprintf(out,"%s\n",test[i]);
  }
  fclose(out);
  }
  答案是:
  voidfun(char*a)
  {
  inti=0,n=0;
  char*p;
  p=a;
  while(*p==’*’)
  {
  n++;
  p++;
  }
  while(*p)
  {
  a[i]=*p;
  i++;
  p++;
  }
  while(n!=0)
  {
  a[i]=’*’;
  i++;
  n--;
  }
  a[i]=’\0’;
    }
首页 1 2 3 尾页

相关文章