@org.junit.Test public void test(){ String a = "I IOVE CHINA"; if(a.indexOf(" ")>0){ String[] aArr = a.split(" "); for (int i = aArr.length-1; i >=0; i--) { System.out.print(aArr[i]+" "); } }else{ char[] aArr = a.toCharArray(); for (int i = aArr.length-1; i >=0; i--) { System.out.print(aArr[i]); } } }