We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 134f8bc commit 5d9323dCopy full SHA for 5d9323d
src/main/java/com/thealgorithms/stacks/ReverseStringUsingStack.java
@@ -13,12 +13,15 @@ private ReverseStringUsingStack() {
13
public static String reverse(String str) {
14
Stack<Character> stack = new Stack<>();
15
StringBuilder reversedString = new StringBuilder();
16
-
17
- if (str.isEmpty())
+ // Check if the input string is empty
+ if (str.isEmpty()) {
18
return str;
19
+ }
20
+ // Push each character of the string onto the stack
21
for (char i : str.toCharArray()) {
22
stack.push(i);
23
}
24
+ // Pop each character from the stack and append to the StringBuilder
25
while (!stack.isEmpty()) {
26
reversedString.append(stack.pop());
27
0 commit comments