C 實戰練習題目79

題目:字符串排序。html

程序分析:無。學習

程序源代碼:spa

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include <string.h>
 4  
 5 void swap(char*str1,char*str2);
 6 int main()
 7 {
 8     char str1[20],str2[20],str3[20];
 9     printf("請輸入3個字符串,每一個字符串以回車結束!:\n");
10     fgets(str1, (sizeof str1 / sizeof str1[0]), stdin);
11     fgets(str2, (sizeof str2 / sizeof str2[0]), stdin);
12     fgets(str3, (sizeof str3 / sizeof str3[0]), stdin);
13     if(strcmp(str1,str2)>0)swap(str1,str2);
14     if(strcmp(str2,str3)>0)swap(str2,str3);
15     if(strcmp(str1,str2)>0)swap(str1,str2);
16     printf("排序後的結果爲:\n");
17     printf("%s\n%s\n%s\n",str1,str2,str3);
18     return 0;
19 }
20 void swap(char*str1,char*str2)
21 {
22     char tem[20];
23     strcpy(tem,str1);
24     strcpy(str1,str2);
25     strcpy(str2,tem);
26 }

以上實例運行輸出結果爲:code

請輸入3個字符串,每一個字符串以回車結束!:
b
a
t
排序後的結果爲:
a
b
t

感謝你的閱讀,請用心感悟!但願能夠幫到愛學習的你!!分享也是一種快樂!!!請接力。。。htm

點擊查看原文,謝謝!blog

相關文章
相關標籤/搜索