Skip to content

Commit 5d9323d

Browse files
committed
Add ReverseStringUsingStack utility for reversing strings using stack
1 parent 134f8bc commit 5d9323d

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

src/main/java/com/thealgorithms/stacks/ReverseStringUsingStack.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,15 @@ private ReverseStringUsingStack() {
1313
public static String reverse(String str) {
1414
Stack<Character> stack = new Stack<>();
1515
StringBuilder reversedString = new StringBuilder();
16-
17-
if (str.isEmpty())
16+
// Check if the input string is empty
17+
if (str.isEmpty()) {
1818
return str;
19+
}
20+
// Push each character of the string onto the stack
1921
for (char i : str.toCharArray()) {
2022
stack.push(i);
2123
}
24+
// Pop each character from the stack and append to the StringBuilder
2225
while (!stack.isEmpty()) {
2326
reversedString.append(stack.pop());
2427
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy