全国等级考试二级C语言2009上机模拟4
一、填空题
请补充函数fun(char *t),该函数的功能是把字符串中的内容逆置。
例如,字符串中原有的字符串为ABCDE,则调用该函数后,串中的内容为EDCBA。
请勿改动主函数main与其他函数中的任何内容,仅在fun函数的横线上填写所需的若干表达式或语句。
注意:部分源程序给出如下。
# include
# include
# include
# define M 60
void fun(char *t)
{
  int j,m=strlen(t)-1;
  char s;
  for(j=0;j  {
    s=t[j];
    ___2___;
    ___3___;
  }
}
main()
{
  char b[M];
  printf("Input a string:");
  gets(b);
  printf("The original string is:");
  puts(b);
  fun(b);
  printf("\n");
  printf("The reversal string :");
  puts(b);
}
二、改错题
下列给定程序中,函数fun的功能是:判断字符ch是否与s所指串中的某个字符相同,若相同,则什么也不做;若不同,则将其插在串的最后。
请修改程序中的错误,使它能得出正确的操作。
注意:不要改动main函数,不能增行或删行,也不能更改程序的结构。
# include
# include
# include
/*******error*********/
void fun(char s,char c)
{
  while(*s && *s!=c)
    s++;
  /*******error*********/
  if(*s=='c')
  {
    s[0]=c;
    /*******error*********/
    s[1]='0';
  }
}
main()
{
  char str[81],ch;
  printf("\n Please enter a string:\n");
  gets(str);
  printf("\n Please enter the character
to search:");
  ch=getchar();
  fun(str,ch);
  printf("\nThe result is %s\n",str);
}
三、编程题
请编写函数fun,函数的功能是求出二维数组周边元素之和,作为函数值返回。二维数组中的值在主函数中赋予。
例如,若二维数组中的值为:
3 5 7 9
9 9 9 4
9 9 9 8
则函数值为72。
请勿改动主函数main与其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。
注意:部分源程序给出如下。
# include
# include
# define M 3
# define N 4
int fun(int b[M][N])
{   
 
}
main()

  int a[M][N]={{3,5,7,9},{9,9,9,4},
{9,9,9,8}};
  int i,j,sum;
  FILE *out;
  printf("The original data is : \n");
  for(i=0;i  {
    for(j=0;j      printf("m",a[i][j]);
    printf("\n");
  }
  sum=fun(a);
  printf("\nThe sum:  %d\n",sum);
  printf("\n");
  out=fopen("outfile.dat","w");
  fprintf(out,"%d",sum);
  fclose(out);
}