In Java Write a recursive method to print a String backwards

In Java

Write a recursive method to print a String backwards. Your method should have the following header. Note: you can write a second, helper method if you choose.

public void printBackwards(String s)

Solution

public class HelloWorld{
public static String printBackwards(String str) //function to print string backwards
{   
if ((str==null)||(str.length() <= 1) ) //base condition
return str;
return printBackwards(str.substring(1)) + str.charAt(0); //calling recursively
}

public static void main(String []args){
String str=\"String for printing backward\"; //input string
String printstr=printBackwards(str); //calling printBackwards function
  
System.out.println(\"String printed from backward : \"+printstr);
}
}
//end of program

/****OUTPUT*****
String printed from backward : drawkcab gnitnirp rof gnirtS   
*****OUTPUT********/

/* Note:This code has been tested on eclipse please ask in case of any doubt,Thanks. */

In Java Write a recursive method to print a String backwards. Your method should have the following header. Note: you can write a second, helper method if you c

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site