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 2 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.thealgorithms.stacks;

import java.util.Stack;

public final class ReverseStringUsingStack {
private ReverseStringUsingStack() {
}

/**
* @param str string to be reversed using stack
* @return reversed string
*/
public static String reverse(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();
}
}
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 reverse4(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();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.thealgorithms.stacks;

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

import org.junit.jupiter.api.Test;

class ReverseStringUsingStackTest {

@Test
void testRegularString() {
assertEquals("olleh", ReverseStringUsingStack.reverse("hello"));
}

@Test
void testEmptyString() {
assertEquals("", ReverseStringUsingStack.reverse(""));
}

@Test
void testPalindromeString() {
assertEquals("madam", ReverseStringUsingStack.reverse("madam"));
}

@Test
void testSpecialCharacters() {
assertEquals("#@!321cba", ReverseStringUsingStack.reverse("abc123!@#"));
}

@Test
void testSingleCharacter() {
assertEquals("x", ReverseStringUsingStack.reverse("x"));
}

@Test
void testWhitespaceHandling() {
assertEquals("dlroW olleH", ReverseStringUsingStack.reverse("Hello World"));
}

@Test
void testNullInput() {
assertThrows(IllegalArgumentException.class, () -> { ReverseStringUsingStack.reverse(null); });
}
}
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 testReverseString4(String input, String expectedOutput) {
assertEquals(expectedOutput, ReverseString.reverse4(input));
}

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