We are writing a program using Recursion method for Reversing a String.
Code:
setyBavaJevoL
Code:
public class RecursiveReverseStr {
public static void main(String args[])
{
System.out.println(reverseStr("LoveJavaBytes"));
}
public static String reverseStr(String str)
{
String reverse="";
/* if (str.length() == 1)
return str;
else {
reverse = reverse + str.charAt(str.length() - 1)
+ reverseStr(str.substring(0, str.length() - 1));
return reverse;
}*/
if(str.length() >= 1)
{
reverse = reverse + str.charAt(str.length() - 1)
+ reverseStr(str.substring(0, str.length() - 1));
}
return reverse;
}
}
Output :setyBavaJevoL
No comments:
Post a Comment