--44 offer wins the series. Flip word order of the columns

Q: cow off the recent arrival of a new employee Fish, every morning and always will be holding a magazine in English, write some sentences in the book. Cat Fish wrote to colleagues interested in the content, look to the day he borrowed Fish, but not read its meaning. For example, "student. A am I" . Later I realized that this guy had the sentence word order reversed, the correct sentence should be "I am a student.". Cat on the order of every one of these words do not flip the line, can you help him?
A: proficient reverse function

    string ReverseSentence(string str) {
        if (str.empty())
            return str;
        int index1 = 0;
        int index2 = 0;
        reverse(str.begin(), str.end());
        while (index1 < str.size()) {
            while (str[index2] != ' ' && index2 < str.size())
                index2++;
            reverse(str.begin() + index1, str.begin() + index2);
            index2++;
            index1 = index2;
        }
        return str;
    }

Guess you like

Origin www.cnblogs.com/xym4869/p/12336362.html