題目描述
將一個英文語句以單詞爲單位逆序排放。例如“I am a boy”,逆序排放後爲“boy a am I”
所有單詞之間用一個空格隔開,語句中除了英文字母外,不再包含其他字符
接口說明
/**
- 反轉句子
- @param sentence 原句子
- @return 反轉後的句子
*/
public String reverse(String sentence);
輸入描述:
將一個英文語句以單詞爲單位逆序排放。
輸出描述:
得到逆序的句子
示例1
輸入
I am a boy
輸出
boy a am I
分析:
利用stack
解答:
#include <iostream>
#include <string>
#include <stack>
using namespace std;
int main(){
string str;
stack<string> ss;
while(cin >> str){
ss.push(str);
}
while(!ss.empty()){
cout << ss.top();
ss.pop();
if(!ss.empty()){
cout << ' ';
}
}
return 0;
}