Skip to content
Home » Blog » C Program to Reverse a Sentence Using Recursion

C Program to Reverse a Sentence Using Recursion

C Program to Reverse a Sentence Using Recursion

In this Program, you’ll learn how to Reverse a Sentence Using Recursion.

To properly understand this Program to Reverse a Sentence Using Recursion you should know the following:

  • C Functions
  • C User-defined functions
  • C Recursion

The below Program takes the input from the user and displays it in reverse order.

Program to Reverse a Sentence Using Recursion

#include <stdio.h>

void reverse();
void main()
{
    printf("Please enter a sentence: ");
    reverse();
}

void reverse()
{
    char c;
    scanf("%c", &c);
    if (c != '\n') {
        reverse();
        printf("%c", c);
    }
}

When you run the program, the output will be:

Please Enter the Sentance: awesome program 
margorp emosewa
C Program to Reverse a Sentence Using Recursion
follow us on Instagram @coderforevers.

This program prints “Please Enter the Sentence: ” on the output screen and then takes input from the user.

When Please “Enter the Sentence is printed” Then, immediately reverse() function called and function stores the first letter entered by user and stores in variable c.

If that variable is something else than ‘\n’ [enter character] then, the reverse() function is called again. Then, the second character is stored in variable c of second reverse function. This process continues until the user enters ‘\n’.

When, user enters ‘\n’, the last function reverse() function returns to second last reverse() function and prints the last character.

Second last reverse() function returns to the third last reverse() function and prints second the last character.

This process goes on and the final output will be the reversed sentence.

Related Program:

  1. C Program to Find ASCII Value of a Character
  2. C Program to Swap Two Numbers
  3. C Program to Demonstrate the Working of Keyword long
  4. C Program to Find the Size of int, float, double and char
  5. C Program to Multiply two Floating Point Numbers
  6. C Program for Stack
  7. C Program to implement Circular Linked List

Ask your questions about how to find Average of Subjects in C and clarify your/others doubts by commenting. Documentation.

Please write to us at [email protected] to report any issue with the above content or for feedback.