StringBuffer sb = new StringBuffer("this is a apple"); //不使用split方法 输入 apple a is this function reverse(StringBuffer sb){ //方法中该如何写? }
走同样的路,发现不同的人生
public static void main(String[] args) { StringBuffer sb = new StringBuffer("this is a apple"); System.out.println(reverse(sb)); } public static StringBuffer reverse(StringBuffer sb) { StringBuffer ret = new StringBuffer(); char[] charArray = sb.toString().toCharArray(); StringBuffer temp = new StringBuffer(); for(int i = charArray.length-1; i >= 0; i--) { if ((charArray[i] >= 'a' && charArray[i] <= 'z') || (charArray[i] >= 'A' && charArray[i] <= 'Z')) { temp.append(charArray[i]); if (i==0) { ret.append(temp); } } else { ret.append(temp); ret.append(" "); temp.delete(0, temp.length()); } } return ret; }
走同样的路,发现不同的人生