Given an input string, reverse the string word by word.spa
For example,
Given s = "the sky is blue
",
return "blue is sky the
".code
click to show clarification.blog
class Solution { public: void reverseWords(string &s) { int nLen=s.size(); if(nLen<=0) return; string temp,result; int index=0; while(s[index]==' ') index++; for(int i=index;i<nLen;) { if(s[i]!=' ') { temp+=s[i]; i++; } else { while(s[i]==' ') i++; if(i==nLen) break; result=" "+temp+result; temp=""; } } s=temp+result; } };