《剑指offer》-翻转单词顺序列

/*
 * 翻转单词顺序列
 * 例如,“student. a am I”
 * 正确的句子应该是“I am a student.”
 * 注意辨别空串和空格串!
 */
public class ReverseSentence {
	 public String reverseSentence(String str) {
		 if(str == null)	return null;	//判断str是否为空
         if(str.trim().equals(""))		return str;		//判断str是否为空格串
         
		 String correctStr = new String();
	     String strArray[] = str.split(" ");
	     
	     for(int i = strArray.length - 1; i >= 0; i --) {
	    	 correctStr = correctStr + strArray[i] + " ";
	     }
	     
	     return correctStr.trim();
	 }
	 
	public static void main(String[] args) {
		String str = new ReverseSentence().reverseSentence(new String(" "));
		System.out.println(str);
	}
}

点赞