STL真的好用!!STL中没有split()函数,需要自己手动实现(这一点差评)!
#include <algorithm>
#include <cctype>
class Solution {
public:
// 自定义转换规则
static char myfun(char ch){
if(isalpha(ch)){
// 是字母
if(islower(ch)){
ch = toupper(ch);
}else{
ch = tolower(ch);
}
}
return ch;
}
// 字符串分割
vector<string> split(string str, string delim){
vector<string> res;
int pos = str.find(delim);
while(pos != -1){
res.push_back(str.substr(0, pos));
str = str.substr(pos + 1);
pos = str.find(delim);
}
if(!str.empty()){
res.push_back(str);
}
return res;
}
string trans(string s, int n) {
// 大小写转换
transform(s.begin(), s.end(), s.begin(), myfun);
vector<string> arrStr = split(s, " ");
reverse(arrStr.begin(), arrStr.end());
// 以空格进行拼接
string res = "";
for(int i = 0; i < arrStr.size(); i ++){
res += arrStr[i];
if(i == arrStr.size() - 1){
continue;
}
res += " ";
}
if(s[s.size() - 1] == ' '){
// cout << "Yes " <<endl;
res.insert(0, " ");
}
return res;
}
};



京公网安备 11010502036488号