zoukankan      html  css  js  c++  java
  • C 实战练习题目79

    题目:字符串排序。

    程序分析:无。

    程序源代码:

     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个字符串,每个字符串以回车结束!:
    ");
    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("排序后的结果为:
    ");
    17     printf("%s
    %s
    %s
    ",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 }

    以上实例运行输出结果为:

    请输入3个字符串,每个字符串以回车结束!:
    b
    a
    t
    排序后的结果为:
    a
    b
    t

    感谢你的阅读,请用心感悟!希望可以帮到爱学习的你!!分享也是一种快乐!!!请接力。。。

    点击查看原文,谢谢!

  • 相关阅读:
    phpcms页面替换
    phpcms笔记
    php头像上传预览
    phpcms后台管理
    php写流程管理
    php写留言板
    php人员权限管理(RBAC)
    单例模式
    Effective C++笔记——day01
    C++Primer笔记-----day08
  • 原文地址:https://www.cnblogs.com/kangyifan/p/13414919.html
Copyright © 2011-2022 走看看