習題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