Skip to content

Commit 09b9f94

Browse files
committed
Refactored ReverseStringUsingStack
1 parent f065fea commit 09b9f94

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-0
lines changed

src/main/java/com/thealgorithms/strings/ReverseString.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.thealgorithms.strings;
22

3+
import java.util.Stack;
4+
35
/**
46
* Reverse String using different version
57
*/
@@ -57,4 +59,31 @@ public static String reverse3(String string) {
5759
}
5860
return sb.toString();
5961
}
62+
/**
63+
* Reverses the given string using a stack.
64+
* This method uses a stack to reverse the characters of the string.
65+
* * @param str The input string to be reversed.
66+
* @return The reversed string.
67+
*/
68+
public static String reverse4(String str) {
69+
// Check if the input string is null
70+
if (str == null) {
71+
throw new IllegalArgumentException("Input string cannot be null");
72+
}
73+
Stack<Character> stack = new Stack<>();
74+
StringBuilder reversedString = new StringBuilder();
75+
// Check if the input string is empty
76+
if (str.isEmpty()) {
77+
return str;
78+
}
79+
// Push each character of the string onto the stack
80+
for (char ch : str.toCharArray()) {
81+
stack.push(ch);
82+
}
83+
// Pop each character from the stack and append to the StringBuilder
84+
while (!stack.isEmpty()) {
85+
reversedString.append(stack.pop());
86+
}
87+
return reversedString.toString();
88+
}
6089
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.thealgorithms.stacks;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class ReverseStringUsingStackTest {
9+
10+
@Test
11+
void testRegularString() {
12+
assertEquals("olleh", ReverseStringUsingStack.reverse("hello"));
13+
}
14+
15+
@Test
16+
void testEmptyString() {
17+
assertEquals("", ReverseStringUsingStack.reverse(""));
18+
}
19+
20+
@Test
21+
void testPalindromeString() {
22+
assertEquals("madam", ReverseStringUsingStack.reverse("madam"));
23+
}
24+
25+
@Test
26+
void testSpecialCharacters() {
27+
assertEquals("#@!321cba", ReverseStringUsingStack.reverse("abc123!@#"));
28+
}
29+
30+
@Test
31+
void testSingleCharacter() {
32+
assertEquals("x", ReverseStringUsingStack.reverse("x"));
33+
}
34+
35+
@Test
36+
void testWhitespaceHandling() {
37+
assertEquals("dlroW olleH", ReverseStringUsingStack.reverse("Hello World"));
38+
}
39+
40+
@Test
41+
void testNullInput() {
42+
assertThrows(IllegalArgumentException.class, () -> { ReverseStringUsingStack.reverse(null); });
43+
}
44+
}

src/test/java/com/thealgorithms/strings/ReverseStringTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,10 @@ public void testReverseString2(String input, String expectedOutput) {
3131
public void testReverseString3(String input, String expectedOutput) {
3232
assertEquals(expectedOutput, ReverseString.reverse3(input));
3333
}
34+
35+
@ParameterizedTest
36+
@MethodSource("testCases")
37+
public void testReverseString4(String input, String expectedOutput) {
38+
assertEquals(expectedOutput, ReverseString.reverse4(input));
39+
}
3440
}

0 commit comments

Comments
 (0)