Files
Java/src/main/java/com/thealgorithms/strings/ReverseStringRecursive.java
Alex Klymenko 0c8616e332 test: ReverseStringRecursiveTest (#5407)
* test: ReverseStringRecursiveTest

* checkstyle: fix formatting

* checkstyle: fix formatting

---------

Co-authored-by: alxkm <alx@alx.com>
2024-08-27 10:49:20 +02:00

22 lines
452 B
Java

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);
}
}
}