Skip to content

Commit 8213586

Browse files
author
“gozhort”
committed
refactor: move reverse method to a dedicated class
1 parent 7ab6393 commit 8213586

File tree

4 files changed

+27
-29
lines changed

4 files changed

+27
-29
lines changed

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

+21
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,25 @@ public static String reverse2(String str) {
3636
}
3737
return new String(value);
3838
}
39+
40+
/**
41+
* Reverse version 3 the given string using a StringBuilder.
42+
* This method converts the string to a character array,
43+
* iterates through it in reverse order, and appends each character
44+
* to a StringBuilder.
45+
*
46+
* @param string The input string to be reversed.
47+
* @return The reversed string.
48+
*/
49+
public static String reverse3(String string) {
50+
if (string.isEmpty()) {
51+
return string;
52+
}
53+
char[] chars = string.toCharArray();
54+
StringBuilder sb = new StringBuilder();
55+
for (int i = string.length() - 1; i >= 0; i--) {
56+
sb.append(chars[i]);
57+
}
58+
return sb.toString();
59+
}
3960
}

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

-21
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,4 @@ public static String reverse(String str) {
1818
return reverse(str.substring(1)) + str.charAt(0);
1919
}
2020
}
21-
22-
/**
23-
* Reverses the given string using a StringBuilder.
24-
* This method converts the string to a character array,
25-
* iterates through it in reverse order, and appends each character
26-
* to a StringBuilder.
27-
*
28-
* @param string The input string to be reversed.
29-
* @return The reversed string.
30-
*/
31-
public static String reverseUsingStringBuilder(String string) {
32-
if (string.isEmpty()) {
33-
return string;
34-
}
35-
char[] chars = string.toCharArray();
36-
StringBuilder sb = new StringBuilder();
37-
for (int i = string.length() - 1; i >= 0; i--) {
38-
sb.append(chars[i]);
39-
}
40-
return sb.toString();
41-
}
4221
}

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

-8
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,4 @@ public class ReverseStringRecursiveTest {
1313
testReverseString(String input, String expectedOutput) {
1414
assertEquals(expectedOutput, ReverseStringRecursive.reverse(input));
1515
}
16-
17-
@ParameterizedTest
18-
@CsvSource({"'Hello World', 'dlroW olleH'", "'helloworld', 'dlrowolleh'", "'123456789', '987654321'", "'', ''", "'A', 'A'", "'!123 ABC xyz!', '!zyx CBA 321!'", "'Abc 123 Xyz', 'zyX 321 cbA'", "'12.34,56;78:90', '09:87;65,43.21'", "'abcdEFGHiJKL', 'LKJiHGFEdcba'",
19-
"'MixOf123AndText!', '!txeTdnA321fOxiM'"})
20-
public void
21-
testReverseUsingStringBuilder(String input, String expectedOutput) {
22-
assertEquals(expectedOutput, ReverseStringRecursive.reverseUsingStringBuilder(input));
23-
}
2416
}

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

+6
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,10 @@ public void testReverseString(String input, String expectedOutput) {
2525
public void testReverseString2(String input, String expectedOutput) {
2626
assertEquals(expectedOutput, ReverseString.reverse2(input));
2727
}
28+
29+
@ParameterizedTest
30+
@MethodSource("testCases")
31+
public void testReverseString3(String input, String expectedOutput) {
32+
assertEquals(expectedOutput, ReverseString.reverse3(input));
33+
}
2834
}

0 commit comments

Comments
 (0)