Skip to content

Commit b7fbbcd

Browse files
committed
Add ReverseStringUsingStack
1 parent 451ed75 commit b7fbbcd

File tree

4 files changed

+119
-0
lines changed

4 files changed

+119
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.thealgorithms.stacks;
2+
3+
import java.util.Stack;
4+
5+
public final class ReverseStringUsingStack {
6+
private ReverseStringUsingStack() {
7+
}
8+
9+
/**
10+
* @param str string to be reversed using stack
11+
* @return reversed string
12+
*/
13+
public static String reverse(String str) {
14+
// Check if the input string is null
15+
if (str == null) {
16+
throw new IllegalArgumentException("Input string cannot be null");
17+
}
18+
Stack<Character> stack = new Stack<>();
19+
StringBuilder reversedString = new StringBuilder();
20+
// Check if the input string is empty
21+
if (str.isEmpty()) {
22+
return str;
23+
}
24+
// Push each character of the string onto the stack
25+
for (char ch : str.toCharArray()) {
26+
stack.push(ch);
27+
}
28+
// Pop each character from the stack and append to the StringBuilder
29+
while (!stack.isEmpty()) {
30+
reversedString.append(stack.pop());
31+
}
32+
return reversedString.toString();
33+
}
34+
}

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: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.thealgorithms.strings;
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
45

56
import java.util.stream.Stream;
67
import org.junit.jupiter.api.Test;
@@ -32,4 +33,15 @@ public void testReverseString2(String input, String expectedOutput) {
3233
public void testReverseString3(String input, String expectedOutput) {
3334
assertEquals(expectedOutput, ReverseString.reverse3(input));
3435
}
36+
37+
@ParameterizedTest
38+
@MethodSource("testCases")
39+
public void testReverseString4(String input, String expectedOutput) {
40+
assertEquals(expectedOutput, ReverseString.reverse4(input));
41+
}
42+
43+
@Test
44+
public void testReverseString4WithNullInput() {
45+
assertThrows(IllegalArgumentException.class, () -> ReverseString.reverse4(null));
46+
}
3547
}

0 commit comments

Comments
 (0)