C语言:将s所指字符串中下标为偶数同时ASCII值为奇数的字符删去,-将a所指字符串中的字符和b所指字符串中的字符的顺序交叉,-将形参s所指字符串中的所有数字字符顺序前移,

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

 1 #include <stdio.h>
 2 #include <string.h>
 3 
 4 void fun(char  *s, char  t[])
 5 {
 6     int i=0,j=0;
 7     while (s[i] != '\0')
 8     {
 9         if (i % 2 == 0)
10         {
11             if ((int)(s[i]) % 2 == 1)//判断ASCII值,使用(int)强制转换类型。
12             {
13                 //printf("%d", (int)s[i]);//调试语句
14                 i++;
15             }
16             else
17             {
18                 t[j] = s[i];
19                 //printf("%c", *t);
20                 i++; j++;
21             }
22         }
23         else
24         {
25             t[j] = s[i];
26             //printf("%c", *t);
27             i++;  j++;
28         }
29     }
30     t[j] = '\0';//切记,切记,在赋值一个新数组的时候要记得添加结束符。
31 }
32 
33 void main()
34 {
35   char   s[100], t[100];void NONO ();
36   printf("\nPlease enter string S:"); scanf("%s", s);
37   fun(s, t);
38   printf("\nThe result is: %s\n", t);
39   NONO();
40 }
41 
42 void NONO ()
43 {/* 本函数用于打开文件,输入数据,调用函数,输出数据,关闭文件。 */
44   char s[100], t[100] ;
45   FILE *rf, *wf ;
46   int i ;
47 
48   rf = fopen("in.dat","r") ;
49   wf = fopen("out.dat","w") ;
50   for(i = 0 ; i < 10 ; i++) {
51     fscanf(rf, "%s", s) ;
52     fun(s, t) ;
53     fprintf(wf, "%s\n", t) ;
54   }
55   fclose(rf) ;
56   fclose(wf) ;
57 }

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

 1 #include <stdio.h>
 2 #include <string.h>
 3 
 4 void fun( char  *a, char  *b, char  *c )
 5 {
 6   int   i , j;     char   ch;
 7   i = 0;    j = strlen(b)-1;//指向尾部
 8 /************found************/
 9   while ( i < j )//首尾对应交换顺序。
10   {   ch = b[i]; b[i] = b[j]; b[j] = ch;
11       i++;    j--;
12   }
13   while ( *a || *b ) {//一个一个进行放置
14 /************found************/
15      if ( *a )
16        { *c = *a;  c++; a++; }
17      if ( *b )
18        { *c = *b;  c++; b++; }
19   }
20   *c = 0;//结束符
21 }
22 
23 void main()
24 {
25   char   s1[100],s2[100],t[200];
26   printf("\nEnter s1 string : ");scanf("%s",s1);
27   printf("\nEnter s2 string : ");scanf("%s",s2);
28   fun( s1, s2, t );
29   printf("\nThe result is : %s\n", t );
30 }

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

 1 #include  <stdio.h>
 2 #include  <string.h>
 3 #include  <stdlib.h>
 4 #include  <ctype.h>
 5 char *fun(char  *s)
 6 { int  i, j, k, n;    char  *p, *t;
 7   n=strlen(s)+1;//求出长度
 8   t=(char*)malloc(n*sizeof(char));//申请内存
 9   p=(char*)malloc(n*sizeof(char));
10   j=0; k=0;
11   for(i=0; i<n; i++)
12   {  if(isdigit(s[i])) {//函数判断是否为数字字符。
13 /**********found**********/
14        p[j]=s[i]; j++;}
15      else
16      {  t[k]=s[i]; k++; }
17    }
18 /**********found**********/
19    for(i=0; i<k; i++) p[j+i]= t[i];//添加在p数组后面
20    p[j+k]=0;//结束符
21 /**********found**********/
22    return p;
23 }
24 void main()
25 { char  s[80];
26   printf("Please input: ");  scanf("%s",s);
27   printf("\nThe result is: %s\n",fun(s));
28 }

//第一题标准答案。

1 {
2      int i,j=0;
3      for(i=0;i<strlen(s);i++)
4      if(!((i%2)==0&&(s[i]%2)))
5        t[j++]=s[i];
6      t[j]=0;
7 }

猜你喜欢

转载自www.cnblogs.com/ming-4/p/10455583.html