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);
}
}
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