如何将给定的字符按字母顺序排列,并将大写字母放在前面。

问题描述 投票:0回答:1

会有一个用户给定的输入,我必须把它按字母顺序排列,大写字母在前,小写字母在后。C++例如,输入是GoodMorning,输出是GMdginnooor。

c++
1个回答
1
投票

很简单,用下面的方法就可以了。

#include <iostream>
#include <algorithm>

int main(void)
{
    std::string text;

    std::cout << "Input a string: ";
    getline(std::cin, text);

    std::sort(text.begin(), text.end());

    std::cout << text;

    return 0;
}

输出示例

Input a string: GoodMorning
GMdginnooor

希望对你有所帮助!

© www.soinside.com 2019 - 2024. All rights reserved.