package stack;

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

public class ReverseApp {

  class StackX{//使用数组模拟栈结构
    private int maxSize;
    private char[] stackArray;
    private int top;
    
    public StackX(int size){
      stackArray = new char[size];
      maxSize = size;
      top = -1;
    }
    
    public char pop(){
      return stackArray[top--];
    }
    
    public void push(char c){
      stackArray[++top] = c;
    }
    
    public char peek(){
      return stackArray[top];
    }
    
    public boolean isEmpty(){
      return top == -1;
    }
    
  }    
  class Reverse{    
    private String input;    
    public Reverse(String in){
      input = in;        
    }    
    public String doRev(){//1:放到栈中;2根据栈后进先出特点,将栈中的数据全部顺序取出
      int len = input.length();
      StackX stack = new StackX(len);
      for(int i =0;i<len;i++){
        stack.push(input.charAt(i));
      }
        
      String output = "";
      while(!stack.isEmpty()){
        output += stack.pop();
      }    
      return output;
    }
  }

  public static void main(String args[]) throws IOException{
    String input =null;
    ReverseApp app = new ReverseApp();

      System.out.println("请输入翻转的字符串");
      System.out.flush();
      input = getString();
      System.out.println(input);    
      Reverse reverse =app.new Reverse(input);
    
  }
    
  public static String getString() throws IOException{//从标准流中获取字符串
    
    InputStreamReader isr = new InputStreamReader(System.in);
    BufferedReader    br = new BufferedReader(isr);
    String s = br.readLine();
    return s;
  }
}
 
 
通过使用栈的后进先出的特点。
现将字符串解析成字符数组,然后放到栈中,顺序取出。