1. 程式人生 > >C語言:將ss所指字符串中所有下標為奇數位置的字母轉換為大寫-將該字符串中的所有字符按ASCII碼值升序排序後輸出。-將a所指的4*3矩陣第k行的元素與第0行元素交換。

C語言:將ss所指字符串中所有下標為奇數位置的字母轉換為大寫-將該字符串中的所有字符按ASCII碼值升序排序後輸出。-將a所指的4*3矩陣第k行的元素與第0行元素交換。

[] sorting 一個 最後一個元素 lib include ascii碼 mes %s

//函數fun:將ss所指字符串中所有下標為奇數位置的字母轉換為大寫,若不是字母,則不轉換。

 1 #include<conio.h>
 2 #include<stdio.h>
 3 #include<string.h>
 4 #include<stdlib.h>
 5 void fun(char *ss)
 6 {
 7     int i;
 8     for (i = 0; ss[i]; i++)
 9     {
10         if (i % 2 == 1)
11         {
12             if
(ss[i] >= a&&ss[i] <= z) 13 { 14 ss[i] -= 32;//轉換成大寫 15 } 16 } 17 } 18 } 19 void main() 20 { 21 FILE *wf; 22 char tt[81],s[81]="abc4Efg"; 23 system("CLS"); 24 printf("\nPlease enter an string within 80 characters:\n
"); 25 gets(tt); 26 printf("\n\nAfter changing, the string\n %s",tt); 27 fun(tt); 28 printf("\nbecomes\n %s\n",tt); 29 /******************************/ 30 wf=fopen("out.dat","w"); 31 fun(s); 32 fprintf (wf,"%s",s); 33 fclose(wf); 34 /*****************************/ 35 }

//函數fun功能:讀入一個字符串,將該字符串中的所有字符按ASCII碼值升序排序後輸出。

 1 #include  <string.h>
 2 #include  <stdio.h>
 3 void fun(char  t[])
 4 {
 5  char c;
 6  int i,j;
 7  /*************found**************/
 8  for(i=strlen(t)-1;i;i--)指向最後一個元素
 9      for(j=0;j<i;j++)
10         /*************found**************/
11       if(t[j]>t[j+1])
12           {
13            c= t[j];
14            t[j]=t[j+1];
15            t[j+1]=c;
16           }
17 }
18 void main()
19 {
20  char  s[81];
21  
22  printf("\nPlease  enter a character string :");
23  gets(s);
24  printf("\n\nBefore sorting :\n  %s",s);
25  fun(s);
26  printf("\nAfter sorting decendingly:\n %s",s);
27 }

//函數fun:將a所指的4*3矩陣第k行的元素與第0行元素交換。

 1 #include  <stdio.h>
 2 #define   N   3
 3 #define   M   4
 4 /**********found**********/
 5 void fun(int (*a)[N], int k)
 6 { int i,temp ;
 7 /**********found**********/
 8   for(i = 0 ; i < N ; i++)
 9   { temp=a[0][i] ;
10 /**********found**********/
11     a[0][i] = a[k][i];
12     a[k][i] = temp ;
13   }
14 }
15 void main()
16 { int x[M][N]={ {1,2,3},{4,5,6},{7,8,9},{10,11,12} },i,j;
17   printf("The array before moving:\n\n");
18   for(i=0; i<M; i++)
19   {  for(j=0; j<N; j++) printf("%3d",x[i][j]);
20      printf("\n\n");
21   }
22   fun(x,2);
23   printf("The array after moving:\n\n");
24   for(i=0; i<M; i++)
25   {  for(j=0; j<N; j++) printf("%3d",x[i][j]);
26      printf("\n\n");
27   }
28 }

C語言:將ss所指字符串中所有下標為奇數位置的字母轉換為大寫-將該字符串中的所有字符按ASCII碼值升序排序後輸出。-將a所指的4*3矩陣第k行的元素與第0行元素交換。