栈(stack) 实现字符串反转

package com.test;

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

/**
 * Created by nanzhou on 2017/8/2.
 */
public class ReverseOrder {

    public static void main(String[] args) throws IOException {

        for (; ; ) {
            Stack stack = new Stack();
            System.out.println("please input words ");

            System.out.flush();
            String s = getString();
             for (int i = 0; i < s.length(); i++) {

                stack.push(String.valueOf(s.charAt(i)));
            }

            String result = "";
              while (!stack.empty()) {

                result += stack.pop();
            }
            System.out.println(result);
        }}

    public static String getString() throws IOException {

        InputStreamReader inputStreamReader = new InputStreamReader(System.in);
        BufferedReader reader = new BufferedReader(inputStreamReader);
        String s = reader.readLine();
        return s;
    }


}

你可能感兴趣的:(数据结构)