Skip to content

Add ReverseStringUsingStack #6452

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/main/java/com/thealgorithms/strings/ReverseString.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.thealgorithms.strings;

import java.util.Stack;

/**
* Reverse String using different version
*/
Expand Down Expand Up @@ -57,4 +59,31 @@ public static String reverse3(String string) {
}
return sb.toString();
}
/**
* Reverses the given string using a stack.
* This method uses a stack to reverse the characters of the string.
* * @param str The input string to be reversed.
* @return The reversed string.
*/
public static String reverseStringUsingStack(String str) {
// Check if the input string is null
if (str == null) {
throw new IllegalArgumentException("Input string cannot be null");
}
Stack<Character> stack = new Stack<>();
StringBuilder reversedString = new StringBuilder();
// Check if the input string is empty
if (str.isEmpty()) {
return str;
}
// Push each character of the string onto the stack
for (char ch : str.toCharArray()) {
stack.push(ch);
}
// Pop each character from the stack and append to the StringBuilder
while (!stack.isEmpty()) {
reversedString.append(stack.pop());
}
return reversedString.toString();
}
}
13 changes: 13 additions & 0 deletions src/test/java/com/thealgorithms/strings/ReverseStringTest.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.thealgorithms.strings;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

import java.util.stream.Stream;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
Expand Down Expand Up @@ -31,4 +33,15 @@ public void testReverseString2(String input, String expectedOutput) {
public void testReverseString3(String input, String expectedOutput) {
assertEquals(expectedOutput, ReverseString.reverse3(input));
}

@ParameterizedTest
@MethodSource("testCases")
public void testReverseStringUsingStack(String input, String expectedOutput) {
assertEquals(expectedOutput, ReverseString.reverseStringUsingStack(input));
}

@Test
public void testReverseStringUsingStackWithNullInput() {
assertThrows(IllegalArgumentException.class, () -> ReverseString.reverseStringUsingStack(null));
}
}