习题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:\n");
34     for (i = 0; i < 5; i++)
35     {
36         puts(str[i]);
37     }
38 
39     return 0;
40 }


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM