Java Program: class Recursion { public static void main(String[] args) { System.
ID: 3939963 • Letter: J
Question
Java Program:
class Recursion
{
public static void main(String[] args)
{
System.out.println(reverse(""));
System.out.println(reverse("A"));
System.out.println(reverse("ABCDEF"));
}
public static String reverse(String s)
{
if (s.length() <= 1)
return s;
return reverse(s.substring(1, s.length())) + s.substring(0,1);
}
}
Rewrite the reverse method shown above. Reverse a string by placing its character at the beginning of the reverse of the substring that contains the entire string except for its last character. Test the program.
Also, rewrite the reverse method shown above. Use recursive method in the String Buffer class.
Explanation / Answer
class Recursion
{
public static void main(String[] args)
{
System.out.println(reverse(""));
System.out.println(reverse("A"));
System.out.println(reverse("ABCDEF"));
}
public static String reverse(String s)
{
StringBuffer sb=new StringBuffer(s);
String rev=new String(sb.reverse());
return rev;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.