#include <cstddef>
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param s string字符串
* @param n int整型
* @return string字符串
*/
string trans(string s, int n) {
// write code here
//思路:需要做两个操作。先调整所有字母的大小写,再反序每个单词的位置
if (n == 0) return nullptr; //空字符串则不处理
stack<string> mystack;
string str;
int pos = 0;
for (int i = 0; i < n; i++) {
if (s[i] >= 'a' && s[i] <= 'z') //如果是小写字母就转换为大写字母
s[i] = s[i] - 32;
else if (s[i] >= 'A' && s[i] <= 'Z')
s[i] = s[i] + 32;
if (s[i] == ' ') {
//遇到空格,将空格前的一个字符串单词截取出来放入栈中
mystack.push(s.substr(pos, i - pos));
mystack.push(" ");
pos = i + 1;
}
if (i == n - 1)
mystack.push(s.substr(pos, i - pos + 1));
}
while (!mystack.empty()) {
str += mystack.top();
mystack.pop();
}
return str;
}
};