
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C++ Program to Reverse a Sentence Using Recursion
In this article, we will write a C++ program to reverse a sentence using recursion. Reversing a sentence means changing the order of all its characters in each word, so the first character moves to the end, the last character comes to the front, and the middle characters follow in the opposite order.
Let's understand this with an example:
// Example 1 Input: Welcome to tutorialspoint! // After reversing the sentence Output: !tniopslairotut ot emocleW // Example 2 Input: Learning C++ with tutorialspoint is fun // After reversing the sentence Output: nuf si tniopslairotut htiw ++C gninraeL
Using Recursion to Reverse a Sentence
Recursion is a technique where a function keeps calling itself to solve a smaller part of the problem until it reaches a base case.
To reverse a sentence using recursion, we take one character at a time and call the function again with the rest of the string. Then, as the function returns, we print each character one by one in reverse order.
Here's how we did this:
- First, we create a function called printReversed() that takes a string as input.
- Then, we check if the string is empty. If it is, we stop the recursion by returning from the function. This is our base case.
- Next, if the string is not empty, we call printReversed() with the string excluding its first character. This reduces the problem size each time.
- Finally, after the recursive call returns, we print the first character. This way, the characters are printed in reverse order as the function returns step by step.
C++ program to Reverse a Sentence Using Recursion
Here's a complete C++ program where we reverse a sentence using recursion.
#include <iostream> #include <string> // Function to print the sentence in reverse using recursion void printReversed(const std::string& text) { if (text.empty()) { return; // stop if string is empty } printReversed(text.substr(1)); // call function for remaining string std::cout << text[0]; // print first character after recursive call } int main() { std::string originalSentence = "TutorialsPoint offers clear coding tutorials"; std::cout << "Original sentence: " << originalSentence << std::endl; std::cout << "Reversed sentence: "; printReversed(originalSentence); return 0; }
Once you run the above program, you will get the following output, which shows how the sentence is reversed using recursion:
Original sentence: TutorialsPoint offers clear coding tutorials Reversed sentence: slairotut gnidoc raelc sreffo tnioPslairotuT
Time Complexity: O(n), where n is the length of the string and each character is processed once.
Space Complexity: O(n) because each recursive call adds a new frame to the call stack for storing substring.