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) {
StringBuffer reverse = new StringBuffer();
int len = s.length();
if(len > 0){
int end = 0;
int start = 0;
int length = 0;
while(start < len){
if(s.charAt(start) == 32){
++start;
++end;
}
else {
if(end < len && s.charAt(end) != 32)
++end;
else{
reverse.insert(0, " "+ s.substring(start, end));
start = end;
length += (end - start + 1);
}
}
}
if(length > 0)
reverse = reverse.deleteCharAt(0);
}
return reverse.toString();
}
}