//
So I need to fill my stack with the characters of string and while i pop the characters 1 by 1 they will be added to another resultant string
*import java.util.*;
public class ReverseStringUsingStack {
private static String Reverse(String str) {
int idx=0;
Stack<Character> s = new Stack<Character>();
while(idx< str.length()){
s.push(str.charAt(idx));
}
StringBuilder result = new StringBuilder("");
while(!s.isEmpty()){
char curr = s.pop();
result.append(curr);
}
return result.toString();
}
public static void main(String[] args) {
String str = "abc";
String ans = Reverse(str);
System.out.println(ans);
}
}*
>Solution :
You must increase idx.
while (idx < str.length()) {
s.push(str.charAt(idx));
idx++;
}