C語言:將s所指字符串中下標爲偶數同時ASCII值爲奇數的字符刪去,-將a所指字符串中的字符和b所指字符串中的字符的順序交叉,-將形參s所指字符串中的全部數字字符順序前移,

//函數fun功能:將s所指字符串中下標爲偶數同時ASCII值爲奇數的字符刪去,s所指串中剩餘的字符造成的新串放在t所指的數組中。c++

 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所指的數組,過長的剩餘字符鏈接在數組的尾部。git

 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 }
相關文章
相關標籤/搜索