Following is one way to reverse string using recursion!
#include <stdio.h>
#include <string.h>
void rev_str_recursive(char arr[], size_t iStart, size_t iLast)
{
if( iStart < iLast )
{
//swap
char temp = arr[iStart];
arr[iStart] = arr[iLast];
arr[iLast] = temp;
rev_str_recursive(arr, ++iStart, --iLast);
}
}
void main()
{
char cArray[] = {"A quick brown fox jumps over a lazy dog"};
rev_str_recursive(cArray, 0, strlen(cArray)-1);
}
Answer from AFI on Stack Overflow Top answer 1 of 12
6
Following is one way to reverse string using recursion!
#include <stdio.h>
#include <string.h>
void rev_str_recursive(char arr[], size_t iStart, size_t iLast)
{
if( iStart < iLast )
{
//swap
char temp = arr[iStart];
arr[iStart] = arr[iLast];
arr[iLast] = temp;
rev_str_recursive(arr, ++iStart, --iLast);
}
}
void main()
{
char cArray[] = {"A quick brown fox jumps over a lazy dog"};
rev_str_recursive(cArray, 0, strlen(cArray)-1);
}
2 of 12
5
You need to modify the string, i.e. the input buffer to reverse(), instead of just printing it.
Doing this recursively seems a bit obnoxious, but should of course be possible.
Basically, I guess the printing becomes an assignment, something like this:
- Base: The reversal of an empty string is the empty string.
- Step: The reversal of a string begins by swapping the first and last characters, then recursing over the remainder of the string.
Programiz
programiz.com › c-programming › examples › reverse-sentence-recursion
C Program to Reverse a Sentence Using Recursion
Find the Frequency of Characters in a String · Add Two Matrices Using Multi-dimensional Arrays · Calculate the Sum of Natural Numbers · To understand this example, you should have the knowledge of the following C programming topics: C Functions · C User-defined functions · C Recursion · #include <stdio.h> void reverseSentence(); int main() { printf("Enter a sentence: "); reverseSentence(); return 0; } void reverseSentence() { char c; scanf("%c", &c); if (c != '\n') { reverseSentence(); printf("%c", c); } } Output ·
Videos
10:36
print reverse of string using recursion in c - YouTube
12:18
Reverse of a string using Recursion C program - YouTube
How to Reverse String and Reverse Array (Iterative and Recursive) ...
04:42
Use Recursion To Print String In Reverse | C Programming Example ...
How to reverse a given string using Recursion | C ...
07:05
Program to reverse a string in C | How to reverse a string | #1 ...
w3resource
w3resource.com › c-programming-exercises › recursion › c-recursion-exercise-9.php
C Program: Get reverse of a string - w3resource
This recursively moves through the string until it reaches the end. For each recursive call, the function stores the current character at the i-th index of the ‘revstr ‘ array and increments the i variable. Finally, when the first character is null, the function returns the revstr array which contains the reversed string.
BeginnersBook -
beginnersbook.com › home › c programs › c program to reverse a string using recursion
C program to Reverse a String using recursion
May 20, 2024 - #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 ...
WsCube Tech
wscubetech.com › resources › c-programming › programs › reverse-string
How to Reverse a String in C? (8 Programs)
October 23, 2025 - Learn How to Reverse a String in C with 8 programs, including loops, recursion, and functions. Easy-to-follow examples for beginners and experts!
IncludeHelp
includehelp.com › c-programs › reverse-a-string-using-recursion.aspx
C program to reverse a string using recursion
Reversed string is: !oohaY RUN 2: Enter a string: Google Reversed string is: elgooG RUN 3: Enter a string: Hello, world! Reversed string is: !dlrow ,olleH ... In the above program, we created two functions StrRev() and main() function. The StrRev() is a recursive function, here we reversed ...
Cplusplus
cplusplus.com › forum › beginner › 184744
Reversing a string using recursion - C++ Forum
#include <iostream> #include <string> using namespace std; string reverseRecursion(string str){ if (str.length() == 1) { return str; }else{ return reverseRecursion(str.substr(1,str.length())) + str.at(0); } } int main() { string str; cout<<"Enter the string to reverse : "; getline(cin, str); cout<<"The reversed string is : "<<reverseRecursion(str); return 0; }
Naukri
naukri.com › code360 › library › reverse-a-string-using-recursion
Reverse a String Using Recursion - Naukri Code 360
Almost there... just a few more seconds
Studytonight
studytonight.com › c › programs › recursion › reverse-a-string-using-recursion
C Program to Reverse A String Using Recursion | C Programs | Studytonight
#include<stdio.h> #include<conio.h> // declaring recursive function char* reverse(char* str); void main() { int i, j, k; char str[100]; char *rev; printf("Enter the string:\t"); scanf("%s", str); printf("The original string is: %s\n", str); rev = reverse(str); printf("The reversed string is: ...
Studytonight
studytonight.com › c-programs › c-program-to-reverse-the-string-using-recursion
C Program To Reverse The String Using Recursion - Studytonight
#include <stdio.h> #include <string.h> void rev(char [], int, int); int main() { char str[50]; int value; printf("Enter The String: "); scanf("%s", str); value = strlen(str); rev(str, 0, value - 1); printf("The String After Reversing: %s\n", str); return 0; } void rev(char str[], int index, int value) { char temp; temp = str[index]; str[index] = str[value - index]; str[value - index] = temp; if (index == value / 2) { return; } rev(str, index + 1, value); } ← Perform Quick Sort on a set of Entries using Recursion← PREV