Java Program class Recursion public static void mainString

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.

Solution

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;

}
}

Java Program: class Recursion { public static void main(String[] args) { System.out.println(reverse(\

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site