1 class Solution 2 { 3 public: 4 string reverseWords(string s) 5 { 6 string result; 7 string tmp; 8 9 for(auto c:s) 10 { 11 if(c!=' ') 12 { 13 tmp += c; 14 } 15 else 16 { 17 reverse(tmp.begin(),tmp.end()); 18 result.append(tmp); 19 result += ' '; 20 tmp.clear(); 21 } 22 } 23 reverse(tmp.begin(),tmp.end()); 24 result.append(tmp); 25 return result; 26 } 27 };