字符串反转(Java)

版权声明:禁止侵权,打击盗版! https://blog.csdn.net/ChenGX1996/article/details/81989548

给定一个句子(只包含字母和空格), 将句子中的单词位置反转,单词用空格分割, 单词之间只有一个空格,前后没有空格。 比如: (1) “hello xiao mi”-> “mi xiao hello”

//字符串反转
public class Laa{
    public static void main(String[] args) throws IOException {
    	System.out.println("请输入字符串:");
        BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));
        //readLine() 取得输入的值
        String str = buf.readLine();
        //split() 将字符串按符号分割为数组
        String[] newArray = str.split(" ");
        //.length 求数组长度
        int len = newArray.length;
        for(int i = len-1;i>=0;i--){
            System.out.print(newArray[i]+" ");
        }
    }
}

猜你喜欢

转载自blog.csdn.net/ChenGX1996/article/details/81989548