C++实现三个字符串的排序输出

标题IDE:codeblocks

标题日期:2019/11/29

标题功能:三个自定义字符串的输出

#include <iostream>
#include <cstring>

using namespace std;

void str_swap(char *s1,char *s2)
{
    char *t;
    strcpy(t,s1);
    strcpy(s1,s2);
    strcpy(s2,t);
}

int main()
{
    char s[3][10];
    cout<<"请输入三个字符串";
    cin>>s[0]>>s[1]>>s[2];
    cout<<s[0]<<'\t'<<s[1]<<'\t'<<s[2]<<endl;
    if(strcmp(s[0],s[1])>0)
        str_swap(s[0],s[1]);
    if(strcmp(s[0],s[2])>0)
        str_swap(s[0],s[2]);
    if(strcmp(s[1],s[2])>0)
        str_swap(s[1],s[2]);
    cout<<s[0]<<'\t'<<s[1]<<'\t'<<s[2]<<endl;
    return 0;
}



发布了57 篇原创文章 · 获赞 2 · 访问量 1883

猜你喜欢

转载自blog.csdn.net/weixin_43476969/article/details/103316098