习题8-7 字符串排序

 1 #include <stdio.h>
 2 #include <string.h>
 3 
 4 int main(void)
 5 {
 6     char str[5][80]; //二维数组保存5个字符串
 7     int i, j;
 8 
 9     for (i = 0; i < 5; i++)
10     {
11         scanf("%s", str[i]); //输入5个字符串
12     }
13 
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:
");
34     for (i = 0; i < 5; i++)
35     {
36         puts(str[i]);
37     }
38 
39     return 0;
40 }
原文地址:https://www.cnblogs.com/2018jason/p/12073154.html