Given an input string, reverse the string word by word.
For example,
Given s = "the sky is blue
",
return "blue is sky the
".
class Solution {
public:
void reverseWords(string &s) {
if(s.empty())
return;
const string s1 =string(s);
int length =s.size();
int p1=length-1, p2=length-1;
int num=0;
s.clear();
bool flag=false;
for(int i=length-1; i>=0; i--){
if(isspace(s1[i])) ;
else{
flag = true;
p1=i;
p2=i;
for(;i>=0; i--)
if(isspace(s1[i])) {p1=i;break;}
if(i == -1)
p1=0;
else
p1=p1+1;
s+=s1.substr(p1,p2-p1+1);
s+=' ';
}
}
if(flag)
s.erase(s.size()-1);
}
};