In this tutorial you will learn about the C Program to Reverse a Sentence Using Recursion and its application with practical example.
C Program to Reverse a Sentence Using Recursion
In this tutorial, we will learn to create a C program that will C Program to Reverse a Sentence Using Recursion in C programming.
Prerequisites
Before starting with this tutorial we assume that you are best aware of the following C programming topics:
- Operators in C Programming.
- Usage of Conditional statements.
- Basic Input and Output function in C Programming.
- Basic C programming.
Reverse a Sentence Using Recursion
In c programming, we have many predefined functions to workout with. The c language allows us to take long inputs from the users i.e. statements, these string values in the c programming language can be manipulated with the help of many predefined functions available for working.
The string manipulation functions can be applied to the variables or to be exact on the input statements. Available functions in a c programming language can make it easier to work with the strings.
With the help of these functions, we can reverse a string. Change a string to an upper case string or to a lower case string and many more.
Algorithm:-
1 2 3 4 5 6 7 8 9 |
1. Declaring the variables for the program. 2. Taking the input statement from the user. 3. Send that statement to the function. 4. Passing those variable to if conditional. 5. Printing the result words. |
Program to Reverse a sentence using the recursion:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
#include <stdio.h> void reverseSentence(); int main() { //print meaasge to take input sentence printf("Enter a sentence: "); reverseSentence(); return 0; } void reverseSentence() { //declaring the vareables char c; //Taking Input sentence scanf("%c", &c); if (c != '\n') { //printing reverse sentence reverseSentence(); printf("%c", c); } } |
Output:-
In the above program we have first initialized the required variable.
- c = string type variable that will hold the value of the input sentence.
Input strings from the user.
Scanning the input for the program.
Reverse printing function for the sentence.
Main Function of the program.