Java Program to Reverse a Sentence Using Recursion

Java Program to reverse a sentence using recursion

What is Recursion in java.

A programming approach known as recursion involves a function calling itself to solve a problem. Recursion is a tool that Java programmers use to find solutions to problems that may be divided into smaller, related subproblems.

A base case and a recursive step are the two primary components of a recursive function. The base case, which must be specified to prevent an infinite loop, is the circumstance in which the recursion ends. The calculation in the function is the recursive phase, and it entails invoking the function once more with an altered argument that gets closer to the base case.

To know more about Java Program to Reverse a sentence using Recursion read the complete article.

How to implement Recursion in Java

Recursion in Java can be implemented by defining a method that calls itself with a modified input until a base case is reached.

  1. Identify the base case(s)
    1. Determine the simplest possible inputs for which the answer is already known. 
    2. Return the known answer for the base case.
  2. Define the recursive case
    1. For inputs that are not a base case, call the same method with a modified input.
    2. The modification should bring the input closer to the base case.
  3. Implement the method
    1. Combine the base case and the recursive case in a single method.
    2. Make sure the method ends by reaching the base case.

Let’s look at a Java program where the implementation of Quicksort Algorithm on java is used to perform an operation.

Example: Implementation of Recursion in Java.

Run
public class Main {
  public static void main(String[] args) {
    String sentence = "Java program to reverse a sentence using recursion";
    String reversed = reverse(sentence);
    System.out.println("The reversed sentence is: " + reversed);
  }

  public static String reverse(String sentence) {
    if (sentence.isEmpty())
      return sentence;

    return reverse(sentence.substring(1)) + sentence.charAt(0);
  }
}

Output

The reversed sentence is: noisrucer gnisu ecnetnes a esrever ot margorp avaJ

Example 2 : Implementation of Recursion in Java.

public class Main {

    public static String reverseString(String sentence) {
        int length = sentence.length();
        if (length <= 1) {
            return sentence;
        }
        return reverseString(sentence.substring(1)) + sentence.charAt(0);
    }

    public static void main(String[] args) {
        String sentence = "PrepInsta and PrepInsta Prime ";
        System.out.println("The reversed sentence is: " + reverseString(sentence));
    }
}

Output

The reversed sentence is:  emirP atsnIperP dna atsnIperP

Example 3: how to use Recursion to reverse a Sentence:

Run

public class Main {

    public static String reverseString(String sentence) {
        int length = sentence.length();
        if (length <= 1) {
            return sentence;
        }
        return reverseString(sentence.substring(1)) + sentence.charAt(0);
    }

    public static void main(String[] args) {
        String sentence = "Hello World";
        System.out.println("The reversed sentence is: " + reverseString(sentence));
    }
}

Output

The reversed sentence is: dlroW olleH

Here’s how the recursion works in this example:

  1. The reverseString method is called with the argument “Hello World”.
  2. The first call to reverseString checks the base case, which is length <= 1. Since length is greater than 1, the method continues to the recursive step.
  3. The recursive step calls the reverseString method with the substring of sentence starting from the second character as the argument. In this case, the argument is “ello World”.
  4. The second call to reverseString checks the base case, which is length <= 1. Since length is greater than 1, the method continues to the recursive step.
  5. The recursive step calls the reverseString method with the substring of sentence starting from the second character as the argument. This process continues until length <= 1.
  6. When length <= 1, the base case returns the original sentence.
  7. The intermediate results are combined and the final result of the reversed sentence, “dlroW olleH”, is returned.

 

 

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription