#include <cctype> #include <stack> class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param s string字符串 * @param n int整型 * @return string字符串 */ string trans(string s, int n) { string res; for(int i=0;i<n;++i){ if(isupper(s[i])) res+=toLower(s[i]); else if(islower(s[i])) res+=toUpper(s[i]); else res+=s[i]; } std::stack<string> stack; string mystr; for(int i=0;i<n;i++){ if(isalpha(res[i])){ mystr+=res[i]; }else{ if(!s.empty()) stack.push(mystr);//使用栈交换单词次序 mystr.clear(); } if(i==n-1) stack.push(mystr); } mystr.clear(); while(!stack.empty()){ mystr+=stack.top(); stack.pop(); if(!stack.empty()){ mystr+=" "; } } return mystr; } char toLower(char c) { if (c >= 'A' && c <= 'Z') { return c + 32; } return c; } char toUpper(char c) { if (c >= 'a' && c <= 'z') { return c - 32; } return c; } };