棧的逆序

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
 * 棧的逆序--控制檯錄入
 * */
public class ReverseApp {
	
	public static void main(String[] args) throws IOException {
		String input,output;
		while(true){
			System.out.println("Enter a string: ");
			System.out.flush();
			input=getString();
			if(input.equals(""))
				break;
			Reverser theReverser=new Reverser(input);
			output=theReverser.doRev();
			System.out.println("Reversed:" +output);
		}
	}
	
	public static String getString() throws IOException{
		InputStreamReader isr=new InputStreamReader(System.in);
		BufferedReader br=new BufferedReader(isr);
		String s=br.readLine();
		return s;
	}
}


/**
 * 棧的逆序--字符串
 * */
public class Reverser {
	private String input;
	private String output;
	
	public Reverser(String in){
		input=in;
	}
	
	public String doRev(){
		int stackSize=input.length();
		StackY thesStack=new StackY(stackSize);
		
		for(int i=0;i<input.length();i++){
			char ch=input.charAt(i);
			thesStack.push(ch);
		}
		output="";
		while(!thesStack.isEmpty()){
			char ch=thesStack.pop();
			output=output+ch;
		}
		return output;
	}
	
	public static void main(String[] args) {
		String inputstr="ABCDEFGH0123456789";
		String outputstr="";
		for(int j=0;j<inputstr.length();j++){
			char ch=inputstr.charAt(j);
			outputstr=ch+outputstr;
		}
		System.out.println(outputstr);
	}
	
}


發佈了26 篇原創文章 · 獲贊 1 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章