Reverse Words in a String
1 void reverseWords(string &s) { 2 string res = "", tmp = ""; 3 int l = s.length(); 4 int i = 0; 5 while(i < l){ 6 if(s[i] != ' ') 7 tmp += s[i++]; 8 else{ 9 if(tmp != ""){ 10 if(res == "") 11 res = tmp; 12 else{ 13 res = tmp + " " + res; 14 } 15 tmp = ""; 16 } 17 i++; 18 } 19 } 20 if(res == "") 21 res = tmp; 22 else if(tmp != ""){ 23 res = tmp + " " + res; 24 } 25 s = res; 26 }