class Solution { public: void reverseWords(string &s) { string result; int size = s.length(); int length = 0; for (int i = size - 1; i >= 0; i--) { if (s.at(i) != ' ') { length++; continue; } if (length) { result += s.substr(i + 1, length) + ' '; length = 0; } } if (length) { result += s.substr(0, length); s = result; } else { s = result.substr(0, result.size() - 1); } } };