-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
45 lines (37 loc) · 860 Bytes
/
Main.java
File metadata and controls
45 lines (37 loc) · 860 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
41
42
43
44
45
import java.util.Scanner;
import java.util.Stack;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Stack st = new Stack();
String s = sc.nextLine();
boolean isReverse = true;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == '<') {
printStack(st);
isReverse = false;
System.out.print(s.charAt(i));
} else if (s.charAt(i) == '>') {
isReverse = true;
System.out.print(s.charAt(i));
} else if (!isReverse) {
System.out.print(s.charAt(i));
} else {
if (s.charAt(i) == ' ') {
printStack(st);
System.out.print(s.charAt(i));
} else {
st.push(s.charAt(i));
}
}
}
printStack(st);
sc.close();
}
static void printStack(Stack st) {
while (!st.empty()) {
System.out.print(st.peek());
st.pop();
}
}
}