Question
Print Reverse of given sentence using recursion
ENTER THE SENTENCE
the rain stopped
REVERSE OF SENTENCE the rain stopped IS deppots niar eht
Share code with your friends
Share on whatsapp
Share on facebook
Share on twitter
Share on telegram
Code
import java.util.Scanner;
public class ReverseOfSentence
{
public static String FindReverse(String sen,int i)
{
if(i>=0)
{
return sen.charAt(i)+ FindReverse(sen,i-1);
}
else
{
return "";
}
}
public static void main()
{
String sentence="";
Scanner sc=new Scanner(System.in);
System.out.println("ENTER THE SENTENCE");
sentence=sc.nextLine();
System.out.print("REVERSE OF SENTENCE "+sentence+" IS "+FindReverse(sentence,sentence.length()-1));
}
}