C program to Reverse a Sentence Using Recursion

@codevarsity
 · 
2.1k views
 · 

In this program takes a sentence from user and reverses that sentence using recursion. This program does not use string to reverse the sentence or store the sentence.

Code

/* Example to reverse a sentence entered by user without using strings. */

#include <stdio.h>
void reverseSentence();

int main()
{
    printf("Enter a sentence: ");
    reverseSentence();

    return 0;
}

void reverseSentence()
{
    char c;
    scanf("%c", &c);

    if( c != '\n')
    {
        reverseSentence();
        printf("%c",c);
    }
}

Output

Enter a sentence: margorp emosewa
awesome program

Comments