题解 | #字符串排序# 字符串stl的使用
字符串排序
https://www.nowcoder.com/practice/d9aa3894d3aa4887843a85d26daa4437
#include <algorithm> #include <iostream> using namespace std; string s; char res[1000]; bool cmp(char a, char b) { if(a >= 'A' && a <= 'Z') a += 32; if(b >= 'A' && b <= 'Z') b += 32; return a < b; } bool isletter(char a) { if(a >= 'A' && a <= 'Z' || (a >= 'a' && a <= 'z')) return true; return false; } int main() { while(getline(cin, s)) { string t = ""; for(int i = 0; i < s.size(); i++) { if(isletter(s[i])) t += s[i]; } stable_sort(t.begin(), t.end(), cmp); for(int i = 0; i < s.size(); i++) { if(!isletter(s[i])) t.insert(t.begin() + i, s[i]); } cout << t << endl; } }