-
Notifications
You must be signed in to change notification settings - Fork 170
/
Copy pathReverseApp.java
executable file
·93 lines (73 loc) · 1.78 KB
/
ReverseApp.java
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import java.util.Scanner;
class Stack {
private Character[] a;
private int ele;
private int size;
public Stack(int size) {
a = new Character[size];
this.size = size;
ele = -1;
}
public void push(Character ch) {
if (isFull()) {
System.out.println("Stack is Full");
} else {
ele++;
a[ele] = ch;
}
}
public Character pop() {
if (isEmpty()) {
System.out.println("Stack is Empty");
return '\0';
} else {
Character ch = a[ele];
ele--;
return ch;
}
}
public boolean isEmpty() {
if(ele == -1) return true;
else return false;
}
public boolean isFull() {
if(ele == size - 1) return true;
return false;
}
}
class Reverser {
private String input;
private String output;
public Reverser(String in) {
input = in;
}
public String doRev() {
int stackSize = input.length();
Stack theStack = new Stack(stackSize);
for(int j=0; j<input.length(); j++) {
char ch = input.charAt(j);
theStack.push(ch);
}
output = "";
while( !theStack.isEmpty() ) {
char ch = theStack.pop();
output = output + ch;
}
return output;
}
}
class ReverseApp {
public static void main(String[] args) {
String input, output;
Scanner sc = new Scanner(System.in);
System.out.println("Enter a string: ");
input = sc.nextLine();
if( input == "" ) {
System.out.println("");
} else {
Reverser theReverser = new Reverser(input);
output = theReverser.doRev();
System.out.println("Reversed: " + output);
}
}
}