Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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;

}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote