String Reverse Using Recursion in Java!!
class main
{
public static void main(String args[])
{
string str="hey";
reverseStr = reverseRecursively(str);
public static String reverseRecursively(String str)
{
//base case to handle one char string and empty string
if (str.length() < 2)
{
return str;
}
return reverseRecursively(str.substring(1)) + str.charAt(0);
}
System.out.println("Reverse String in Java using Recursion: " + reverseStr);
}
}
No comments:
Post a Comment