-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram110.java
More file actions
62 lines (50 loc) · 2.39 KB
/
Program110.java
File metadata and controls
62 lines (50 loc) · 2.39 KB
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
import java.util.Stack;
// Program to reverse a string using a stack
public class Program110 {
// Function to reverse a string using a stack
public static String reverse(String input) {
// Create an empty stack of characters
Stack<Character> stack = new Stack<>();
/*
Step: Push all characters from input string to stack
Input: "hello world"
Stack Contents after push:
['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd']
(Top of stack is 'd')
*/
// Step 1: Push all characters of the string into the stack
for (int i = 0; i < input.length(); i++) {
stack.push(input.charAt(i)); // Push each character
}
/*
Step-by-step Pop and build reversed string:
Initial Output: ""
Pop 1st time: Stack: [..., 'l'] → Popped: 'd' → Output: "d"
Pop 2nd time: Stack: [..., 'r'] → Popped: 'l' → Output: "dl"
Pop 3rd time: Stack: [..., 'o'] → Popped: 'r' → Output: "dlr"
Pop 4th time: Stack: [..., 'w'] → Popped: 'o' → Output: "dlro"
Pop 5th time: Stack: [..., ' '] → Popped: 'w' → Output: "dlrow"
Pop 6th time: Stack: [..., 'o'] → Popped: ' ' → Output: "dlrow "
Pop 7th time: Stack: [..., 'l'] → Popped: 'o' → Output: "dlrow o"
Pop 8th time: Stack: [..., 'l'] → Popped: 'l' → Output: "dlrow ol"
Pop 9th time: Stack: [..., 'e'] → Popped: 'l' → Output: "dlrow oll"
Pop 10th time: Stack: [..., 'h'] → Popped: 'e' → Output: "dlrow olle"
Pop 11th time: Stack: [] → Popped: 'h' → Output: "dlrow olleh"
Final Output: "dlrow olleh"
*/
// Step 2: Pop characters from stack and append to result
StringBuilder reversed = new StringBuilder();
while (!stack.isEmpty()) {
reversed.append(stack.pop()); // Pop from stack (LIFO)
}
// Return the reversed string
return reversed.toString();
}
// Main method to test the function
public static void main(String[] args) {
String original = "hello world";
String reversed = reverse(original);
System.out.println("Original: " + original); // Output: hello world
System.out.println("Reversed: " + reversed); // Output: dlrow olleh
}
}