Using a stack to reverse a string.

Question:
How to reverse a String using a stack?

Answer:
Here is an example using the java Stack class using the push(), pop() and empty() methods.

Code:
import java.util.*;
 
public class StackExample {
   public static void main(String[] args) {
 
      Scanner sc = new Scanner(System.in);
      System.out.print("Enter string: ");
      String str = sc.nextLine();
 
      Stack<Character> stack = new Stack<Character>();
 
      for(int i=0; i<str.length(); i++){
         stack.push(str.charAt(i));
      }
      while(! stack.empty()){
        System.out.print(stack.pop()); 
      }
      System.out.println(""); 
   }
}

Output:
$ java StackExample
Enter string: I love java
avaj evol I