/* 思路:字符串处理 1. 使用getline获取输入字符串 2. 复制一份输入的字符串,备份字符串只保留大小写字母 3. 使用双重for循环遍历。 第一个for循环遍历26个字母,第二个for循环遍历字符串,把字符串按规则押入到队列中 4. 与原来的字符串比较。 原字符串是字母位置的地方用队列中的字符替换 */ #include <cctype> #include<iostream> #include <vector> using namespace std; string handle_str(string & str){ string ans = str; vector<char> tv; int len = ans.length(); for(int i = 0; i < 26; i++){ for(int j = 0; j < len; j++){ if(( ans[j] - 'a' == i) || ( ans[j] -'A' == i)){ tv.push_back(ans[j]); } } } int vLen = tv.size(); for(int i = 0, j = 0; i < len && j < vLen; i++){ if(isalpha(ans[i])){// 替换原字符串是字母的位置 ans[i] = tv[j]; j++; } } return ans; } int main(){ string str; getline(cin , str); cout << handle_str(str) << endl; return 0; }