模拟试题

3773考试网计算机等级考试模拟试题正文

2014年3月全国计算机二级C语言上机模拟试题四十二之3

来源:2exam.com 2013-10-30 10:18:13


  解题思路: 

  第一处:for循环语句中缺少分号。 

  第二处:应该把pstr+j位置上的值赋值给*(pstr+i)上,所以应改为: 

  *(pstr+i)=*(pstr+j);。 

  *************************************************** 

  请编写函数fun,它的功能是:求出ss所指字符串中指定字符的个数,并返回此值。 

  例如,若输入字符串: 123412132,输入字符为: 1,则输出:3。 

  注意: 部分源程序在文件PROG1.C中。 

  请勿改动主函数main和其它函数中的任何内容,仅在函数fun的花括号中填入你编写的若干语句。 

  给定源程序: 

  #include  

  #include  

  #define M 81 

  int fun(char *ss, char c) 

  { 

  } 

  main() 

  { char a[M], ch; 

  printf("\nPlease enter a string:"); gets(a); 

  printf("\nPlease enter a char:"); ch = getchar(); 

  printf("\nThe number of the char is: %d\n", fun(a, ch)); 

  NONO ( ); 

  } 

  解题思路: 

  本题是考察考生怎样从字符串中找出指定字符的个数。使用while循环语句来字符串是否结束,如果没有结束,则判断当前字符是否是指定的字符,如果是指定的字符,则个数cnt加1,直至字符串结束为止,最后由函数返回。 

  参考答案: 

  int fun(char *ss, char c) 

  { 

  int cnt = 0 ; 

  char *p = ss ; 

  while(*p) { 

  if(*p == c) cnt++ ; 

  p++ ; 

  } 

  return cnt ; 

  } 

触屏版 电脑版
3773考试网 琼ICP备12003406号-1