Created
December 24, 2021 08:14
-
-
Save gauravkukade/16388aabb97a4990bd91cd32f3acf9d6 to your computer and use it in GitHub Desktop.
A java program to reverse a string using recursion. Read more at https://coderolls.com/java-reverse-string-using-recursion/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* A java program to reverse a string using recursion. | |
* | |
* @author gaurav | |
* | |
*/ | |
public class ReverseStringUsingRecursion { | |
public static void main(String[] args) { | |
String str = "hello"; | |
System.out.println(reverse(str)); // olleh | |
} | |
public static String reverse(String str) { | |
if ((null == str) || (str.length() <= 1)) { | |
return str; | |
} | |
return reverse(str.substring(1)) + str.charAt(0); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment