. 1 #include <stdio.h> 2 #include < String .h> . 3 . 4 int main ( void ) . 5 { . 6 char STR [ . 5 ] [ 80 ]; // a two-dimensional array holding a string 5 . 7 int I, J ; . 8 . 9 for (I = 0 ; I < . 5 ; I ++ ) 10 { . 11 Scanf ( " % S " , STR [I]); // input string 5 12 is } 13 is 14 for (i = 0; i < 4; i++) 15 { 16 int index = i; 17 for (j = i + 1; j < 5; j++) 18 { 19 if (strcmp(str[index], str[j]) > 0) 20 { 21 index = j; 22 } 23 } 24 if (i != index) 25 { 26 char t[80]; 27 strcpy(t, str[index]); //交换字符串 28 strcpy(str[index], str[i]); 29 strcpy(str[i], t); 30 } 31 } 32 33 printf("After sorted:\n"); 34 for (i = 0; i < 5; i++) 35 { 36 puts(str[i]); 37 } 38 39 return 0; 40 }
Exercise 8-7 string sorting
Guess you like
Origin www.cnblogs.com/2018jason/p/12073154.html
Ranking