-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathreverseStringStackGFG.java
More file actions
40 lines (34 loc) · 949 Bytes
/
reverseStringStackGFG.java
File metadata and controls
40 lines (34 loc) · 949 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// { Driver Code Starts
/*package whatever //do not write package name here */
import java.util.*;
class reverseStringStackGFG {
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-- > 0){
Solution obj = new Solution();
System.out.println(obj.reverse(sc.next()));
}
}
}
// } Driver Code Ends
class Solution {
public void push(Stack<Character> stack, char item)
{
stack.push(item);
}
public char pop(Stack<Character> stack)
{
return stack.pop();
}
public String reverse(String S){
//code here
Stack<Character> stack = new Stack<Character>();
String revStr = new String();
for(int i=0; i<S.length(); i++)
push(stack, S.charAt(i));
for(int i=0; i<S.length(); i++)
revStr = revStr+pop(stack);
return revStr;
}
}