Respuesta :

Answer:

// here is code in java.

// import package

import java.util.*;

// class definition

class Main

{

// method that return reverse string

   public static String reverseO(String st,int n)

   {

   // create an empty string

       String rev_st="";

       for(int x=n-1;x>=0;x--)

       {

       // append character from last into the empty string

           rev_st=rev_st+st.charAt(x);

       }

       // return the reversed string

       return rev_st;

   }

   // main method of the class

public static void main (String[] args) throws java.lang.Exception

{

   try{

       // scanner object to read string

Scanner scr=new Scanner(System.in);

System.out.print("enter the string:");

 // read the string from user

   String st=scr.nextLine();

   // find the length of string

   int len=st.length();

   // call the method with parameter string and length

   System.out.println("reverse string is: "+reverseO(st,len));

   }catch(Exception ex){

       return;}

}

}

Explanation:

Read a string from user and assign it to variable "st" and Find  its length. Then call the method reverseO() with parameter string and length.Then in the method reverseO(), create an empty string.Then append the characters of input string to empty string from last index.Then return the string.This will be the string in reverse order.

Output:

enter the string:hello

reverse string is: olleh

tonb

Answer:

See the other answer for the solution in Java. However, to make the reversing truly recursive, replace it by this:

 public static String reverseO(String st,int n)

  {

   String ret = st.substring(n-1,n);

   if (n > 1)  

     ret += reverseO(st, n-1);

   return ret;

 }

Explanation:

A recursive function calls itself. The function places the last character at the start and then calls itself to do the same on the remainder of the string. Make sure there always is a stop criterium, to end the recursion.

Recursive functions are often elegant, but most of the times not very efficient in terms of stack usage.