-
Notifications
You must be signed in to change notification settings - Fork 0
/
Reverse_String.java
51 lines (42 loc) · 1021 Bytes
/
Reverse_String.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
//WAP to reverse String Using Stack
import java.util.*;
class Stacks3 {
int top;
int size;
char sa[];
Stacks3(int c) {
size = c;
top = -1;
sa = new char[size];
}
void push(char a) {
if (top >= size - 1) {
System.out.println("Stack is Overflow ");
} else {
top++;
sa[top] = a;
}
}
void pop() {
if (top == -1) {
System.out.println("Stack Underflow !");
} else {
top--;
System.out.print(sa[top + 1]);
}
}
}
class Run1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter String to Reverse: ");
String str = sc.nextLine();
Stacks3 s1 = new Stacks3(str.length());
for (int i = 0; i < str.length(); i++) {
s1.push(str.charAt(i));
}
for (int i = 0; i < str.length(); i++) {
s1.pop();
}
}
}