给定一个字符串,逐个翻转字符串中的每个单词。
示例:
输入: "the sky is blue",
输出: "blue is sky the".
说明:
无空格字符构成一个单词。
输入字符串可以在前面或者后面包含多余的空格,但是反转后的字符不能包括。
如果两个单词间有多余的空格,将反转后单词间的空格减少到只含一个。
package com.qianfeng.test;
import java.util.Scanner;
public class test3 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
chuLi(s);
String[] a = s.split(" ");
fanZhuan(a);
}
public static void fanZhuan(String[] a){
for(int i= a.length-1;i>=0;i--){
System.out.print(a[i]+" ");
}
System.out.println();
}
public static void chuLi(String s) {
s.trim();
s.replaceAll("{2,}", " ");//正则表达式
}
}