C Program to Reverse a String Using Recursion

Introduction

Reversing a string means rearranging the characters of the string such that the first character becomes the last, the second character becomes the second-to-last, and so on. This guide will show you how to write a C program to reverse a string using recursion.

Example:

  • Input: "hello"
  • Output: "olleh"

Problem Statement

Create a C program that:

  • Takes a string as input from the user.
  • Uses a recursive function to reverse the string.
  • Displays the reversed string.

Solution Steps

  1. Include the Standard Input-Output and String Libraries: Use #include <stdio.h> for standard input-output functions and #include <string.h> for string manipulation.
  2. Write the Recursive Function: Define a recursive function that reverses the string by swapping characters.
  3. Write the Main Function: Define the main function to take user input, call the recursive function, and display the reversed string.
  4. Input the String: Use scanf or gets to take input from the user for the string.
  5. Call the Recursive Function: Pass the string and its indices to the recursive function to reverse it.
  6. Display the Reversed String: Use printf to display the reversed string.

C Program to Reverse a String Using Recursion

#include <stdio.h> #include <string.h> // Step 2: Define the recursive function to reverse the string void reverseString(char str[], int start, int end) { if (start >= end) { return; // Base case: When start index is greater than or equal to end index } // Swap the characters at start and end indices char temp = str[start]; str[start] = str[end]; str[end] = temp; // Recursively call the function with the next set of indices reverseString(str, start + 1, end - 1); } int main() { // Step 3: Declare a variable to hold the string char str[100]; // Step 4: Prompt the user to enter the string printf("Enter a string: "); gets(str); // Using gets to read the string including spaces // Step 5: Call the recursive function to reverse the string reverseString(str, 0, strlen(str) - 1); // Step 6: Display the reversed string printf("Reversed string: %s\n", str); return 0; // Return 0 to indicate successful execution } 

Explanation

Step 2: Define the Recursive Function

  • The reverseString function takes three parameters:
    • str[]: The string to be reversed.
    • start: The starting index for the current recursion step.
    • end: The ending index for the current recursion step.
  • Base Case: If the start index is greater than or equal to the end index, the recursion stops, as the string has been fully reversed.
  • Recursive Case:
    • Swap the characters at the start and end indices.
    • Call reverseString recursively with the start index incremented by 1 and the end index decremented by 1.

Step 3: Declare a Variable

  • The variable str is declared to store the input string.

Step 4: Input the String

  • The program prompts the user to enter a string using gets, which reads the entire line of input, including spaces.

Step 5: Call the Recursive Function

  • The program calls the reverseString function with the string, the starting index 0, and the ending index strlen(str) - 1.

Step 6: Display the Reversed String

  • The program uses printf to display the reversed string.

Return 0

  • The return 0; statement indicates that the program executed successfully.

Output Example

Example 1:

Enter a string: hello Reversed string: olleh 

Example 2:

Enter a string: C programming Reversed string: gnimmargorp C 

Conclusion

This C program demonstrates how to reverse a string using a recursive function. It covers basic concepts such as recursion, string manipulation, and user input, making it a useful example for beginners learning C programming.

Leave a Comment

Scroll to Top