class Solution {
public:
    string ReverseSentence(string str) {
        stack<string> s;
        stringstream in(str);
        string a;
        while(in >> a)
            s.push(a);
        a = "";
        while(!s.empty()) {
            a += s.top();
            if(s.size() != 1) a += " ";
            s.pop();
        }
        return a;
    }
};