Reverse a string using recursion


Reversing a string means changing the order of its characters so that the last character becomes the first, the second-to-last becomes the second, and so on. While there are various ways to accomplish this, using recursion in C++ can be an elegant and educational choice.

How Does Recursive String Reversal Work?

The recursive approach involves breaking down the problem into smaller, more manageable parts. In the case of reversing a string, we can think of it as:

 

  1. Removing the last character.
  2. Reversing the remaining characters.
  3. Appending the removed character at the end.


This process continues until the string becomes empty. Let's dive into the C++ implementation.

 

C++ Code
#include <iostream>
#include <string>

std::string reverseString(std::string inputStr) {
   // Base case: an empty string or a string with one character is already reversed
   if (inputStr.length() <= 1) {
       return inputStr;
   } else {
       // Recursively reverse the substring without the last character
       return reverseString(inputStr.substr(1)) + inputStr[0];
   }
}

int main() {
   std::string originalStr = "Hello, World!";
   std::string reversedStr = reverseString(originalStr);
   
   std::cout << "Original: " << originalStr << std::endl;
   std::cout << "Reversed: " << reversedStr << std::endl;
   
   return 0;
}

 

Code Explanation

Let's break down the code step by step:

  • We include the necessary header files, <iostream> and <string>, for input and string operations.
  • The reverseString function takes a string inputStr as its parameter.
  • In the base case, we check if the length of inputStr is less than or equal to 1. If so, it's already reversed, and we return it.
  • In the recursive case, we call reverseString with a substring of inputStr that excludes the first character using inputStr.substr(1). We then append the first character to the end of the reversed substring.
  • In the main function, we create an original string, "Hello, World!".
  • We call the reverseString function to reverse the string and store the result in reversedStr.
  • Finally, we print the original and reversed strings.

 

Example Usage and Output

Output

 

Original: Hello, World!
Reversed: !dlroW ,olleH

 

As you can see, the string "Hello, World!" has been successfully reversed using a recursive approach in C++.

 

The time and space complexity of reversing a string recursively using the provided code are as follows:

  • Time Complexity: The time complexity of this recursive approach is O(n), where 'n' is the length of the input string. The reason for this is that, in each recursive call, we are removing one character from the input string by taking a substring (excluding the first character). Since we call the function recursively 'n' times (for 'n' characters), the time complexity is linear.
  • Space Complexity: The space complexity of this recursive approach is O(n) as well. This is because for each recursive call, a new substring is created. In the worst case, when the recursion is at its deepest level (i.e., the base case is reached), there will be 'n' recursive calls on the call stack, each with its own substring. This results in O(n) space usage.

 

Summary

 

Reversing a string using recursion in C++ is an excellent example of how recursion can break down a problem into simpler subproblems. It's a valuable exercise for those new to programming and algorithms. While not the most efficient method for long strings, it offers a deeper understanding of the power of recursion in problem-solving.



Thanks for feedback.