题目要求
思路
1.首先根据ASCII的规则,把字符串大小写替换,空格保持不变
2.将整个字符串进行翻转
3.以空格为区间,将区间内的字符串进行翻转,其中翻转的函数reverse()
代码实现
class Solution {
public:string trans(string s, int n) {string str = "";for(int i = 0; i < n; i++){if(s[i] >= 'A' && s[i] <= 'Z')str += s[i] - 'A' + 'a';else if(s[i] >= 'a' && s[i] <= 'z')str += s[i] - 'a' + 'A';elsestr += s[i];}reverse(str.begin(), str.end());for(int i = 0; i < n; i++){int j = i;while(j < n && str[j] != ' ')j++;reverse(str.begin()+i, str.begin()+j);i = j;}return str;}
};