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

考试站(www.examzz.com)   【考试站:中国教育考试第一门户】   2013年4月25日
  编程题
  请编写一个函数fun(char *s)该函数的功能是把字符串中的内容逆置。
  例如:字符串中原有的字符串为abcdefg,则调用该函数后,串中的内容为gfedcba.
  注意:部分源程序给出如下。
  请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。
  试题程序:
  #include <string.h>
  #include <conio.h>
  #include <stdio.h>
  #define N 81
  void fun ( char *s)
  {
  }
  main()
  {
  char a[N];
  FILE *out;
  printf ( "Enter a string : ");
  gets ( a );
  printf ( "The original string is: " );
  puts( a );
  fun ( a );
  printf("\n");
  printf ( "The string after modified : ");
  puts ( a );
  strcpy(a, "Hello World!");
  fun(a);
  out = fopen("out.dat", "w");
  fprintf(out, "%s" , a);
  fclose(out);
  }
  答案是:
  
void fun (char *s)
  {
  char ch;
  int i,m,n;
  i=0;
  m=n=strlen(s)-1;
  while(i<(n+1)/2)
  {
  ch=s[i];
  s[i]=s[m];
  s[m]=ch;
  i++;
  m--;
  }
  }
首页 1 2 3 尾页

相关文章