1 import java.util.Scanner; 2 3 /** 4 * 功能:字符串翻转,例如how are you 翻转为you are how 5 */ 6 public class Main5 { 7 8 public String swap(String string) { 9 10 if (string == null) { 11 return null; 12 } 13 14 String[] strings = string.split(" "); 15 16 String temp; 17 for (int i = 0; i < strings.length / 2; i++) { 18 temp = strings[i]; 19 strings[i] = strings[strings.length - 1 - i]; 20 strings[strings.length - 1 - i] = temp; 21 } 22 23 StringBuilder stringBuilder = new StringBuilder(); 24 for (int i = 0; i < strings.length; i++) { 25 if (i != 0) { 26 stringBuilder.append(" "); 27 } 28 stringBuilder.append(strings[i]); 29 } 30 31 return stringBuilder.toString(); 32 } 33 34 public static void main(String[] args) { 35 36 Scanner scanner = new Scanner(System.in); 37 Main5 main5 = new Main5(); 38 39 while (scanner.hasNextLine()) { 40 String string = scanner.nextLine(); 41 System.out.println(main5.swap(string)); 42 } 43 } 44 }