Skip to content

Commit a540bc4

Browse files
committed
Refactored ReverseStringUsingStack utility for reversing strings using stack
1 parent 379cb67 commit a540bc4

File tree

4 files changed

+40
-3
lines changed

4 files changed

+40
-3
lines changed

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,18 @@ private ReverseStringUsingStack() {
1111
* @return reversed string
1212
*/
1313
public static String reverse(String str) {
14+
if(str==null) {
15+
throw new IllegalArgumentException("Input string cannot be null");
16+
}
1417
Stack<Character> stack = new Stack<>();
1518
StringBuilder reversedString = new StringBuilder();
1619
// Check if the input string is empty
1720
if (str.isEmpty()) {
1821
return str;
1922
}
2023
// Push each character of the string onto the stack
21-
for (char i : str.toCharArray()) {
22-
stack.push(i);
24+
for (char ch : str.toCharArray()) {
25+
stack.push(ch);
2326
}
2427
// Pop each character from the stack and append to the StringBuilder
2528
while (!stack.isEmpty()) {

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

Lines changed: 28 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,30 @@ public static String reverse3(String string) {
5759
}
5860
return sb.toString();
5961
}
62+
63+
/**
64+
* Reverse version 4 the given string using a Stack.
65+
* This method pushes each character of the string onto a stack
66+
* and then pops them off to create the reversed string.
67+
*/
68+
public static String reverse4(String str) {
69+
if(str==null) {
70+
throw new IllegalArgumentException("Input string cannot be null");
71+
}
72+
Stack<Character> stack = new Stack<>();
73+
StringBuilder reversedString = new StringBuilder();
74+
// Check if the input string is empty
75+
if (str.isEmpty()) {
76+
return str;
77+
}
78+
// Push each character of the string onto the stack
79+
for (char ch : str.toCharArray()) {
80+
stack.push(ch);
81+
}
82+
// Pop each character from the stack and append to the StringBuilder
83+
while (!stack.isEmpty()) {
84+
reversedString.append(stack.pop());
85+
}
86+
return reversedString.toString();
87+
}
6088
}

src/test/java/com/thealgorithms/stacks/ReverseStringUsingStackTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,6 @@ void testWhitespaceHandling() {
3939

4040
@Test
4141
void testNullInput() {
42-
assertThrows(NullPointerException.class, () -> { ReverseStringUsingStack.reverse(null); });
42+
assertThrows(IllegalArgumentException.class, () -> { ReverseStringUsingStack.reverse(null); });
4343
}
4444
}

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)