句子逆序(stringstream实现)

题目描述

将一个英文语句以单词为单位逆序排放。例如“I am a boy”,逆序排放后为“boy a am I”
所有单词之间用一个空格隔开,语句中除了英文字母外,不再包含其他字符

输入例子

I am a boy

输出例子

boy a am I

代码

#include 
#include 
#include 
using namespace std;

int main() {
    string input;
    string res = " ", temp;
    getline(cin, input);
    stringstream ss;
    ss.str(input);
    while (ss >> temp) {
        if (res == " ") {
            res = temp;
        }
        else
            res = temp + " " + res;
    }
    cout << res << endl;
    system("pause");
    return 0;
}

你可能感兴趣的:(华为机试题)