Reverse Words in a String

题目:

Given an input string, reverse the string word by word.

For example,
Given s = "the sky is blue",
return "blue is sky the".

 1 public String reverseWords(String s) {
 2         if(null==s) return null;
 3         StringBuilder sb = new StringBuilder("") ;
 4         String[] vals = s.trim().split(" ");
 5         for(int i=vals.length-1;i>=0;i--){
 6                 
 7             if(!vals[i].equals("")){
 8                 if(i<vals.length-1)
 9                     sb.append(" ");
10                 sb.append(vals[i]);
11             }
12         }
13         return sb.toString();
14     }

 

posted @ 2014-07-12 15:42  月窟仙人  阅读(137)  评论(0编辑  收藏  举报