题解 | 字符串排序
字符串排序
https://www.nowcoder.com/practice/5190a1db6f4f4ddb92fd9c365c944584
#include <iostream> #include <cctype> #include <algorithm> using namespace std; int main() { string str, str1; getline(cin, str); for(const char& c: str) { if(isupper(c) || islower(c)) { str1 += c; } } stable_sort(str1.begin(), str1.end(), [](char a, char b) { char t1 = tolower(a); char t2 = tolower(b); return t1 < t2; }); int slow = 0; for(int fast = 0; fast < str.size(); ++fast) { if(isalpha(str[fast])) { str[fast] = str1[slow++]; } } cout << str << endl; return 0; }