Question
Given an input string, reverse the string word by word.
For example,
Given s = "the sky is blue",
return "blue is sky the".
code:
public class ReverseString {
public static void main(String[] args) {
String str = "the sky is blue";
reverse(str);
}
public static void reverse(String str){
Stack<String> stack = new Stack<String>();
new Stack<Character>();
StringBuffer sb = new StringBuffer();
String[] words = str.split(" ");
for (String word : words) {
stack.push(word);
}
while(!stack.isEmpty()){
String w = stack.pop();
sb.append(w).append(" ");
}
System.out.println(sb);
}