二级C语言操作例题(十三)

一、程序填空题

在此程序中,函数fun的功能是:将形参s所指字符串中的所有数字字符顺序前移,其他字符顺序后移,处理后新字符串的首地址作为函数值返回。

例如,s所指字符串为:asd123fgh543df,处理后新字符串为:123543asdfghdf。

#include  <stdio.h>

#include  <string.h>

#include  <stdlib.h>

#include  <ctype.h>

char *fun(char  *s)

{ int  i, j, k, n;    char  *p, *t;

  n=strlen(s)+1;

  t=(char*)malloc(n*sizeof(char));

  p=(char*)malloc(n*sizeof(char));

  j=0; k=0;

  for(i=0; i<n; i++)

  {  if(isdigit(s[i])) {

/**********found**********/

       p[__1__]=s[i]; j++;}

     else

     {  t[k]=s[i]; k++; }

   }

/**********found**********/

   for(i=0; i<__2__; i++) p[j+i]= t[i];

   p[j+k]=0;

/**********found**********/

   return __3__;

}

void main()

{ char  s[80];

  printf("Please input: ");  scanf("%s",s);

  printf("\nThe result is: %s\n",fun(s));

}

答案:(1) j (2) k (3) p

二、程序修改题

在此程序中,函数fun的功能是:首先把b所指字符串中的字符按逆序存放,然后将a所指字符串中的字符和b所指字符串中的字符,按排列的顺序交叉合并到c所指数组中,过长的剩余字符接在c所指的数组的尾部。

例如,当a所指字符串中的内容为”abcdefg”,b所指字符串中的内容为”1234”时,c所指数组中的内容应该为”1g2f3e4dcba”。

#include <stdio.h>

#include <string.h>

void fun( char  *a, char  *b, char  *c )

{

  int   i , j;     char   ch;

  i = 0;    j = strlen(b)-1;

/************found************/

  while ( i > j )

  {   ch = b[i]; b[i] = b[j]; b[j] = ch;

      i++;    j--;

  }

  while ( *a || *b ) {

/************found************/

     If ( *a )

       { *c = *a;  c++; a++; }

     if ( *b )

       { *c = *b;  c++; b++; }

  }

  *c = 0;

}

void main()

{

  char   s1[100],s2[100],t[200];

  printf("\nEnter s1 string : ");scanf("%s",s1);

  printf("\nEnter s2 string : ");scanf("%s",s2);

  fun( s1, s2, t );

  printf("\nThe result is : %s\n", t );

}

答案:(1) while ( i < j ) (2) if ( *a )

三、程序设计题

在此程序中,函数fun的功能是:将s所指字符串中下标为偶数同时ASCII值为奇数的字符删除,s所指串中剩余的字符形成新串放在t所指的数组中。

例如,若s所指字符串中的内容为”ABCDEFG12345”,其中字符C的ASCII码值为奇数,在数组中的下标为偶数,因此必须删除;而字符1的ASCII码值为奇数,在数组中的下标为奇数,因此不应当删除,其他依此类推。最后t所指的数组中的内容应是”BDF12345”。

#include <stdio.h>

#include <string.h>

void fun(char  *s, char  t[])

{

}

void main()

{

  char   s[100], t[100];void NONO ();

  printf("\nPlease enter string S:"); scanf("%s", s);

  fun(s, t);

  printf("\nThe result is: %s\n", t);

  NONO();

}

void NONO ()

{/* 本函数用于打开文件,输入数据,调用函数,输出数据,关闭文件。 */

  char s[100], t[100] ;

  FILE *rf, *wf ;

  int i ;

  rf = fopen("in.dat","r") ;

  wf = fopen("out.dat","w") ;

  for(i = 0 ; i < 10 ; i++) {

    fscanf(rf, "%s", s) ;

    fun(s, t) ;

    fprintf(wf, "%s\n", t) ;

  }

  fclose(rf) ;

  fclose(wf) ;

}

答案:

int i, j = 0 ;

for(i = 0 ; i < strlen(s) ; i++)

  if(!((i % 2) ==0 && (s[i] % 2))) t[j++] = s[i] ;

t[j] = 0 ;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值