In this post, we will learn how to reverse a sentence using recursion in C Programming language.

This program will take a line of sentence as input from the user, and reverse the sentence using a user-defined function. For example: Hello World becomes dlroW olleH.
So, without further ado, let’s begin this tutorial.
C Program To Reverse a Sentence Using Recursion
// C Program To Reverse a Sentence Using Recursion #include <stdio.h> void reverse(){ char c; // Asking for Input scanf("%c", &c); if (c != '\n'){ reverse(); printf("%c", c); } } int main(){ printf("Enter a Sentence: \n"); reverse(); return 0; }
Output
Enter a Sentence:
This is a Sentence
ecnetneS a si sihT
How Does This Program Work ?
printf("Enter a Sentence: \n");
In this program, the user is asked to enter a line of sentence.
reverse();
After that, our user-defined reverse() function is called.
void reverse(){
char c;
// Asking for Input
scanf("%c", &c);
if (c != '\n'){
reverse();
printf("%c", c);
}
}
This function stored the first letter of the sentence entered by the user in c.
If the variable is any character other than \n, the reverse() function is called again. This process continues until a new line is identified.
When a new line is identified, the reverse() function starts printing the characters from the last.
Conclusion
I hope after going through this post, you understand how to reverse a sentence using recursion in C Programming language.
If you have any query regarding the program, feel free to contact us in the comment section. We will be delighted to help you.
Also Read: