The Wayback Machine - https://web.archive.org/web/20240717181820/https://www.geeksforgeeks.org/java-program-to-reverse-a-sentence-using-recursion/
Open In App

Java Program to Reverse a Sentence Using Recursion

Last Updated : 11 Sep, 2022
Improve
Suggest changes
Post a comment
Like Article
Like
Save
Share
Report

A sentence is a sequence of characters separated by some delimiter. This sequence of characters starts at the 0th index and the last index is at len(string)-1. By reversing the string, we interchange the characters starting at 0th index and place them from the end. The first character becomes the last, the second becomes the second last, and so on. 

Example:

Input : GeeksforGeeks
Output:    skeegrofskeeG

Input : Alice
Output: ecilA

Approach:

  1. Check if the string is empty or not, return null if String is empty.
  2. If the string is empty then return the null string.
  3. Else return the concatenation of sub-string part of the string from index 1 to string length with the first character of a string. e.g. return substring(1)+str.charAt(0); which is for string “Mayur” return will be “ayur” + “M”.

Below is the implementation of the above approach:

Java




// Java Program to Reverse a Sentence Using Recursion
import java.io.*;
 
public class GFG {
    public static String reverse_sentence(String str)
    {
        // check if str is empty
        if (str.isEmpty())
            // return the string
            return str;
        else {
           
            // extract the character at 0th index, that is
            // the character at beginning
            char ch = str.charAt(0);
           
            // append character extracted at the end
            // and pass the remaining string to the function
            return reverse_sentence(str.substring(1)) + ch;
        }
    }
 
    public static void main(String[] args)
    {
        // specify the string to reverse
        String str = "Geeksforgeeks";
       
        // call the method to reverse sentence
        String rev_str = reverse_sentence(str);
       
        // print the reversed sentence
        System.out.println(
            "Sentence in reversed form is :  " + rev_str);
 
        // creating another string with numbers
        // and special characters
        String str2 = "Alice";
       
        String rev_str2 = reverse_sentence(str2);
       
        // print the reversed sentence
        System.out.println(
            "Sentence in reversed form is :  " + rev_str2);
    }
}


Output

Sentence in reversed form is :  skeegrofskeeG
Sentence in reversed form is :  ecilA

Time Complexity: O(N), where N is the length of the string.

Auxiliary Space: O(N) for call stack



Previous Article
Next Article

Similar Reads

Java Program to Find Reverse of a Number Using Recursion
Recursion is a process by which a function calls itself repeatedly till it falls under the base condition and our motive is achieved. To solve any problem using recursion, we should simply follow the below steps: Step 1: Assume the smaller problem from the problem that is similar to the bigger/original problem.Step 2: Decide the answer to the small
5 min read
Java program to swap first and last characters of words in a sentence
Write a Java Program to Swap first and last character of words in a Sentence as mentioned in the example? Examples: Input : geeks for geeks Output :seekg rof seekg Approach:As mentioned in the example we have to replace first and last character of word and keep rest of the alphabets as it is. First we will create an Char array of given String by us
2 min read
Java program to count the characters in each word in a given sentence
Write a Java program to count the characters in each word in a given sentence? Examples: Input : geeks for geeksOutput :geeks->5for->3geeks->5 Recommended: Please solve it on PRACTICE first, before moving on to the solution. Approach:Here we have to find out number of words in a sentence and the corresponding character count of each word.
3 min read
Java Program to Compute the Sum of Numbers in a List Using Recursion
ArrayList is a part of the Collection framework and is present in java.util package. It provides us with dynamic arrays in Java. Though, it may be slower than standard arrays but can be helpful in programs where lots of manipulation in the array is needed. This class is found in java.util package. Illustration: Input : [1, 3, 9] Output : 13 Here th
5 min read
Java Program to Convert Binary Code Into Equivalent Gray Code Using Recursion
Convert the Binary code of the Number into it's equivalent Gray's code using recursion. Binary is the default way to store numbers, but in many applications, binary numbers are not useful and a variation of binary is needed. Gray code has the property that two successive numbers differ in only one bit and used in K-maps, error correction, and commu
3 min read
Java Program to Find Sum of N Numbers Using Recursion
Recursion is a process by which a function calls itself repeatedly till it falls under the base condition and our motive is achieved. To solve any problem using recursion, we should simply follow the below steps: Assume/Identify the smaller problem from the problem which is similar to the bigger/original problem.Decide the answer to the smallest va
4 min read
Java Program to Convert Binary Code into Gray Code Without Using Recursion
Binary Code of a number is the representation of a number in Binary (base-2) number system. In Binary Number System, each number is expressed using only two literals (0 and 1). Each of these literals is called a bit. The binary number system is very useful in digital electronic circuits. Gray Code of a number is the representation of a number using
4 min read
Program to convert first character uppercase in a sentence
Write a program to convert the first character uppercase in a sentence and if apart from the first character if any other character is in Uppercase then convert it into Lowercase. Examples: Input : gEEKsOutput : Geeks Input : GFGOutput : Gfg Input : GeeksforGeeksOutput : Geeksforgeeks Method 1: Iterate through each character in the input string.If
9 min read
Java Program to Reverse a String using Stack
The Stack is a linear data structure that follows the LIFO(Last In First Out) principle, i.e, the element inserted at the last is the element to come out first. Approach: Push the character one by one into the Stack of datatype character.Pop the character one by one from the Stack until the stack becomes empty.Add a popped element to the character
2 min read
Java Program to Reverse a Number and find the Sum of its Digits Using do-while Loop
Problem Statement: The number is supposed to be entered by the user be it any random number lying within the primitive data-type holding the number. First, the number needs to be reversed. Secondary the sum of the number is to be calculated with the constraint to use a do-while loop. do-while loop: Now the user sometimes does get confused between a
6 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg