Migrate recursive reverse string method and tests to ReverseString (#6504)

* Migrate recursive reverse string method and tests to ReverseString

- Moved recursive reverse method from ReverseStringRecursive.java to ReverseString.java
- Moved tests from ReverseStringRecursiveTest.java to ReverseStringTest.java
- Deleted old ReverseStringRecursive files

* Fix formatting for ReverseStringTest.java as per clang-format linter
This commit is contained in:
Nihar Kakani
2025-08-27 20:29:04 +05:30
committed by GitHub
parent e78d53d747
commit 27ada8a649
4 changed files with 22 additions and 37 deletions

View File

@@ -86,4 +86,17 @@ public final class ReverseString {
}
return reversedString.toString();
}
/**
* Reverse the String using Recursion
* @param str string to be reversed
* @return reversed string
*/
public static String reverseStringUsingRecursion(String str) {
if (str.isEmpty()) {
return str;
} else {
return reverseStringUsingRecursion(str.substring(1)) + str.charAt(0);
}
}
}

View File

@@ -1,21 +0,0 @@
package com.thealgorithms.strings;
/**
* Reverse String using Recursion
*/
public final class ReverseStringRecursive {
private ReverseStringRecursive() {
}
/**
* @param str string to be reversed
* @return reversed string
*/
public static String reverse(String str) {
if (str.isEmpty()) {
return str;
} else {
return reverse(str.substring(1)) + str.charAt(0);
}
}
}

View File

@@ -1,16 +0,0 @@
package com.thealgorithms.strings;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
public class ReverseStringRecursiveTest {
@ParameterizedTest
@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'",
"'MixOf123AndText!', '!txeTdnA321fOxiM'"})
public void
testReverseString(String input, String expectedOutput) {
assertEquals(expectedOutput, ReverseStringRecursive.reverse(input));
}
}

View File

@@ -7,6 +7,7 @@ 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.CsvSource;
import org.junit.jupiter.params.provider.MethodSource;
public class ReverseStringTest {
@@ -44,4 +45,12 @@ public class ReverseStringTest {
public void testReverseStringUsingStackWithNullInput() {
assertThrows(IllegalArgumentException.class, () -> ReverseString.reverseStringUsingStack(null));
}
@ParameterizedTest
@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'",
"'MixOf123AndText!', '!txeTdnA321fOxiM'"})
public void
testReverseStringUsingRecursion(String input, String expectedOutput) {
assertEquals(expectedOutput, ReverseString.reverseStringUsingRecursion(input));
}
}