1. 程式人生 > 實用技巧 >字串排序(HJ26)

字串排序(HJ26)

一:解題思路

二:完整程式碼示例 (C++版和Java版)

C++程式碼:

#include <iostream>
#include <vector>
#include <string>

using namespace std;

int main()
{
    string str = "";
    while (cin >> str)
    {
        vector<char> result;
        for (int j = 0; j < 26; j++)
        {
            
for (int i = 0; i < str.size(); i++) { if (str[i] - 'a' == j || str[i] - 'A' == j) { result.push_back(str[i]); } } } int index = 0; for (int i = 0; i < str.size(); i++) {
if ((str[i] >= 'A' && str[i] <= 'Z') || (str[i] >= 'a' && str[i] <= 'z')) { if (index < result.size()) { cout << result[index]; index++; } }
else { cout << str[i]; } } cout << endl; } return 0; }