翻转单词顺序列
代码实现
package 剑指offer;
import java.util.Scanner;
/**
* @author WangXiaoeZhe
* @Date: Created in 2019/11/22 16:19
* @description:
*/
public class Main10 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String s = scanner.nextLine();
System.out.println(ReverseSentence(s));
}
public static String ReverseSentence(String str) {
/**
* 去掉首尾空格
*/
if (str.trim().equals("")) {
return str;
}
String[]a=str.split(" ");
StringBuilder o = new StringBuilder();
for (int i = a.length; i > 0; i--) {
o.append(a[i - 1]);
if (i > 1) {
o.append(" ");
}
}
return o.toString();
}
}