Given an input string, reverse the string word by word.
For example,
Given s = "the sky is blue
",
return "blue is sky the
".
public class Solution {
public String reverseWords(String s) {
s=s.trim();
String re="";
int index=s.length()-1;
String temp="";
while(index>=0){
if(index==0){
temp=Character.toString(s.charAt(index))+temp;
re=re+temp;
break;
}
if(s.charAt(index)==' '){
if(temp==""){
index--;
continue;
}
else{
temp+=' ';
re=re+temp;
temp="";
index--;
continue;
}
}
else{
temp=Character.toString(s.charAt(index))+temp;
index--;
continue;
}
}
return re;
}
}