BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

C program to Reverse a String using recursion

Last Updated: May 20, 2024 by Chaitanya Singh | Filed Under: C Programs

In this article, we will write a C program to reverse a string using recursion. A function calling itself with smaller instances is called recursion. Recursion can be used for problems that can be broken down into smaller, similar problems.

C Program to reverse an input string using recursion

In this program, the recursive function, swaps first and last character of string, it then calls the function itself with smaller string (first and last character removed). The same process happens for the substring, it keeps happening until start >= end.

#include <stdio.h>
#include <string.h>

// Function to reverse a string using recursion
void reverseString(char* str, int start, int end) {
    if (start >= end) {
        return;
    }

    // Swap the characters at start and end
    char temp = str[start];
    str[start] = str[end];
    str[end] = temp;

    // Recursively calling the function with smaller instances
    reverseString(str, start + 1, end - 1);
}

int main() {
    ////This array would hold the string upto 100 char
    char str[100];

    // Read the input string and store into str[]
    printf("Enter a string: ");
    fgets(str, sizeof(str), stdin);

    // Remove newline from input string, if present
    str[strcspn(str, "\n")] = '\0';

    // Get the length of input string
    int len = strlen(str);

    // Calling the function to reverse string
    reverseString(str, 0, len - 1);

    // Print the reversed string
    printf("Reversed string: %s\n", str);

    return 0;
}

Output:

Enter a string: Chaitanya
Reversed string: aynatiahC

Explanation of the Program:

  1. reverseString() Function:
    • It takes three variables as parameters, str is for string, start and end represents the positions of characters in strings that needs to be swapped.
    • The base case for the recursion is when start is greater than or equal to end, at this point, the recursion process ends.
    • In each recursive function call, the characters at positions start and end are swapped, and then the function calls itself with the next set of indices (start + 1 and end - 1).
    • The way the whole string gets reversed and when all the characters are swapped, the base case reaches and the recursion process stops.
  2. Main Function:
    • Read the user entered string using fgets.
    • Remove newline, if present using strcspn.
    • It determines the length of string using strlen.
    • The reverseString function is called with start index as 0 and end index as len - 1.
    • Finally, the reversed string is printed.

Related C Programs:

  • C Program to reverse a given number
  • C Program to swap first occurrence of a character in a String
  • C Program to swap first and last digit of a number
  • Python program to reverse a String using recursion
  • Java Program to reverse a String using recursion

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Comments

  1. Gabriel says

    June 18, 2016 at 1:44 AM

    Hi I think it should be:
    scanf (“%s”, string_array);

    Because the string is already an address (&), right?

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap