題目:字符串排序。
程序分析:無。
程序源代碼:
// Created by www.runoob.com on 15/11/9.
// Copyright © 2015年 菜鳥教程. All rights reserved.
//
#include<stdio.h>
#include<stdlib.h>
void swap(char*str1,char*str2);
int main()
{
char str1[20],str2[20],str3[20];
printf("請輸入3個字符串,每個字符串以回車結束!:\n");
gets(str1);
gets(str2);
gets(str3);
if(strcmp(str1,str2)>0)swap(str1,str2);
if(strcmp(str2,str3)>0)swap(str2,str3);
if(strcmp(str1,str2)>0)swap(str1,str2);
printf("排序後的結果為:\n");
printf("%s\n%s\n%s\n",str1,str2,str3);
return 0;
}
void swap(char*str1,char*str2)
{
char tem[20];
strcpy(tem,str1);
strcpy(str1,str2);
strcpy(str2,tem);
}
以上實例運行輸出結果為:
請輸入3個字符串,每個字符串以回車結束!: b a t 排序後的結果為: a b t