Wednesday, 29 June 2016

Reverse String using Stack Data Structure

 Reverse a String using Stack Data Structure in Java  :


import java.util.Stack;

public class ReverseString {
    public static void main(String args[]) {
        Stack<Character> stack = new Stack<Character>();
        String result="My name is Anurag Goel.";
        char exp[] = obj.next().toCharArray();
        for (char e : exp) {
            stack.push(e);
        }
        while (!stack.isEmpty()) {
            result=result+stack.pop().toString();
        }
        System.out.println(result);
    }

}

No comments:

Post a Comment