码迷,mamicode.com
首页 > 其他好文 > 详细

字符串的逆序

时间:2017-12-04 16:42:12      阅读:170      评论:0      收藏:0      [点我收藏+]

标签:gets   static   log   int   util   ann   字符串   reverse   equals   

public class Reverse {
    private String input;
    private String output;
    
    public Reverse(String in) {
        input = in;
    }
    
    public String doRev() {
        int stackSize = input.length();
        StackX theStack = new StackX(stackSize);
        
        for (int j = 0; j < input.length(); j++) {
            char ch = input.charAt(j);
            theStack.push(ch);
        }
        
        output = "";
        while(!theStack.isEmpty()) {
            char ch = theStack.pop();
            output = output + ch;
        }
        
        return output;
        
    }
}
// -----------分界线-----------
public class StackX {
    private int maxSize;
    private char[] stackArray;
    private int top;
    
    public StackX(int max) {
        maxSize = max;
        stackArray = new char[maxSize];
        top = -1;
    }
    
    public void push(char j) {
        stackArray[++top] = j;
    }
    
    public char pop() {
        return stackArray[top--];
    }
    
    public char peek() {
        return stackArray[top];
    }
    
    public boolean isEmpty() {
        return (top == -1);
    }
    
}
// -----------分界线-----------
import java.util.Scanner;

public class ReverseApp {
    
    public static Scanner sc = new Scanner(System.in);
    
    public static void main(String[] args) {
        String input;
        String output;
        
        while(true) {
            System.out.println("Please Enter a string:");
            input = getString();
            
            if(input.equals("END")) {
                break;
            }
            
            Reverse theReverser = new Reverse(input);
            output = theReverser.doRev();
            System.out.println("Reversed:" + output);
            }
        sc.close();
    }
    
    public static String getString(){
        return sc.next();
    }
}

字符串的逆序

标签:gets   static   log   int   util   ann   字符串   reverse   equals   

原文地址:http://www.cnblogs.com/mmsumz/p/7977527.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!