Program to Reverse a Sentence Using Recursion - ( C Programs )
Example
#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: Hello, World!
!dlroW ,olleH
Explanation
In this C program, we are using recursion to reverse a sentence. In the reverseSentence()
function, we are taking input one character at a time using the scanf()
function. We check if the entered character is a newline character ('\n'). If yes, we move back up the recursive calls using the function call stack and print the characters entered in reverse order.
Use
This program can be used to reverse a sentence and print it out in reverse order. It's a good exercise in understanding recursion and function calls in C programming.
Summary
In this program, we have demonstrated how to use recursion to reverse a sentence in C programming. The program takes input one character at a time and uses recursive function calls to print out the characters in reverse order. This program is a good exercise in learning about recursion and function calls in C programming.