Reverse Words in a String
Given an input string, reverse the string word by word.
For example,
Given s = "the sky is blue
",
return "blue is sky the
".
1 class Solution { 2 public: 3 void reverseWords(string &s) { 4 char c; 5 int i,j,k; 6 i = 0; 7 while(s[i] == ' ') 8 { 9 s.erase(0,1); 10 } 11 int len = strlen(s.c_str()); 12 j = len-1; 13 while (s[j] == ' ') 14 { 15 s.erase(j,1); 16 j--; 17 } 18 len = strlen(s.c_str()); 19 i = 0; 20 j = len-1; 21 while (i < j) 22 { 23 c = s[i]; 24 s[i] = s[j]; 25 s[j] = c; 26 i++; 27 j--; 28 } 29 i = 0; 30 while (true) 31 { 32 if (s[i] == '