Given an input string, reverse the string word by word.
For example,
Given s = "the sky is blue",
return "blue is sky the".
class Solution {
public:
    void reverseWords(string &s) {
        int begin = 0;
        int end = 0;
        
        while(end < s.size()){
            if(s[end] == ' '){
                swapString(s, begin, end - 1);
                begin = end+1;
                end = begin;
            }
            else{
                end++;
            }
        }
        swapString(s, begin, end - 1);
        
        swapString(s, 0, s.size()-1);
    }
    
    void swapString(string &s, int begin, int end){
        while(end > begin){
            char c = s[begin];
            s[begin] = s[end];
            s[end] = c;
            begin++;
            end--;
        }
    }
};class Solution {
public:
    void reverseWords(string &s) {
        stack<int> word;
        stack<int> sentence;
        int i = 0;
        
        while(i <= s.size()){
            if(i == s.size() || s[i] == ' '){
                if(!word.empty()){
                    if(!sentence.empty()){
                        sentence.push(' ');
                    }
                    while(!word.empty()){
                        sentence.push(word.top());
                        word.pop();
                    }
                }
            } else{
                word.push(s[i]);
            }
            i++;
        };
        
        s.clear();
        while(!sentence.empty()){
            s.push_back(sentence.top());
            sentence.pop();
        };
    }
};LeetCode | Reverse Words in a String
原文:http://blog.csdn.net/lanxu_yy/article/details/38827845