题解 | #字符串变形#
字符串变形
http://www.nowcoder.com/practice/c3120c1c1bc44ad986259c0cf0f0b80e
public:
string trans(string s, int n) {
vector<string> res;
string temp="";
s+=" ";
for(char ch:s)
{
if(ch!=' ') ch=islower(ch) ? toupper(ch):tolower(ch);
if(ch==' ')
{
res.push_back(temp);
temp.clear();
}
temp+=ch;
}
s.clear();
//reverse(res.begin(),res.end());
for(int i=res.size()-1;i>=0;i--)
{
if(i==1){
s+=res[i]+' ';
}
else s+=res[i];
}
if(s[0]==' ')
{
s.erase(0,1);
}
return s;
}
};