Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A java program to reverse a string using recursion. Read more at https://coderolls.com/java-reverse-string-using-recursion/
/**
* 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