Core Java小练习 - 反转文字

来源:互联网 发布:linux 写脚本文件 编辑:程序博客网 时间:2024/06/05 16:04

传入一个字符串,得到反转的字符串:

 

import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;

public class ReveserOrder {

 public void order() throws IOException {  InputStreamReader is = new InputStreamReader(System.in);  BufferedReader br = new BufferedReader(is);  String str = br.readLine();  char c[] = str.toCharArray();  int i = str.length();  i--;  char temp;  for (int j = 0; j < i; j++, i--) {   temp = c[j];   c[j] = c[i];   c[i] = temp;  }  System.out.println(new String(c, 0, str.length())); }

 public static void main(String[] args) throws IOException {  new  ReveserOrder().order(); }}

 

import java.io.BufferedReader;import java.io.InputStreamReader;public class ReverserO {  public String reverserStr(String inStr){    char[] charArray = inStr.toCharArray();    int inStrLong = charArray.length;    char[] returnCharArray = new char[inStrLong];    for(int i=inStrLong-1,j=0;i>-1;i--,j++){      returnCharArray[j]=charArray[i];    }    return new String(returnCharArray,0,inStrLong);  }  public static void main(String[] args) throws Exception {    InputStreamReader inputStreamReader = new InputStreamReader(System.in);    BufferedReader br = new BufferedReader(inputStreamReader);    System.out.println(new ReverserO().reverserStr(br.readLine()));  }}