-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStacks.java
More file actions
29 lines (26 loc) · 892 Bytes
/
Stacks.java
File metadata and controls
29 lines (26 loc) · 892 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
package dsa;
import java.util.Stack;
public class Stacks {
public static void main(String[] args) {
String str = "luhar";
//String Reversal.
Stack <Character> stk = new Stack<>();
for(char ch : str.toCharArray()){
stk.push(ch);
}
StringBuffer reversed = new StringBuffer();
while(!stk.empty())
reversed.append(stk.pop());
System.out.println(reversed);
//Matching Braces.
String exp = "[{1 + 2( 3 + 4) }< 5 + 6> (9 -3) {7 * 5} 4 % 8]";
Stack <Character> stack = new Stack<>();
for (char ch : exp.toCharArray()) {
if(ch == '[' || ch == '(' || ch == '{' || ch == '<')
stack.push(ch);
if(ch == ']' || ch == ')' || ch == '}' || ch == '>')
stack.pop();
}
System.out.println(stack.empty());
}
}