| package com.itheima; import java.io.*;
 import java.util.*;
 public class reverse_char {
 //这是我的 两种方法 实现
 
 public static void main(String []args)throws IOException
 {
 System.out.println("请输入你要输入的字符");
 BufferedReader read_line=new BufferedReader(new InputStreamReader(System.in));
 String str=new String();
 while(true)
 {
 str=read_line.readLine();
 System.out.println("你输入的为"+str);
 method_1(str);
 method_2(str);
 }
 
 
 }
 public static void method_1(String str)
 {
 
 String temp[]=str.split("\\s");
 for(int i=temp.length-1;i>=0;i--)
 {
 System.out.println(temp[i]+"");
 }
 
 for(int i=0;i<temp.length;i++)
 {
 System.out.println(temp[i]+"");
 }
 
 }
 
 public static void swap(char[] arr, int begin, int end) {
 while(begin < end) {
 char temp = arr[begin];
 arr[begin] = arr[end];
 arr[end] = temp;
 begin++;
 end--;
 }
 
 public static void method_2(String str) {
 char[] arr = str.toCharArray();
 swap(arr, 0, arr.length - 1);
 int begin = 0;
 for (int i = 1; i < arr.length; i++) {
 if (arr[i] == ' ') {
 swap(arr, begin, i - 1);
 begin = i + 1;
 }
 for (int j = 1; j < arr.length; j++) {
 System.out.println(arr[j]);
 
 }
 
 
 }
 
 }
 |