这道题的本意应该是不让我们用库函数,不过库函数确实太好用了就偷一下懒吧。
public String ReverseSentence(String str) { String[] split = str.split(" "); if(split.length==0) return str; int i = 0; int j = split.length-1; while(i<j){ String temp = split[i]; split[i]=split[j]; split[j]=temp; i++; j--; } str = ""; for (int k = 0; k <split.length ; k++) { if(k==split.length-1){ str+=split[k]; }else{ str+=split[k]+" "; } } return str; }