- 使用栈结构完成字符串的倒序,。
- 使用islower,isupper,isalpha等函数完成相关的大小写判断
#include<iostream>
#include<string>
#include<stack>
using namespace std;
string trans(string s, int n) {
// write code here
stack<string> line;
string word;
for (auto &temp : s) {
if (isspace(temp)) {
if (!word.empty())
line.push(word);
line.push(" ");
word.clear();
}
else if (isalpha(temp)) {//判断如果是字母
if (islower(temp))
temp = toupper(temp);
else if (isupper(temp))
temp = tolower(temp);
word += temp;
}
}
line.push(word);//这句话很关键,如果字符的结尾没有空格的话,要把最后一个单词进栈。
string result,top_str;
while (!line.empty()) { //出栈操作
top_str = line.top();
result = result + top_str;
line.pop();
}
return result;
}
int main() {
string a{ "hello world" };
cout<<trans(a, 12);
return 0;
}