Reverse a string using recursion in c

C code to reverse a string by recursion:

#define MAX 100
char* getReverse(char[]);

int main(){

    char str[MAX],*rev;

    printf("Enter  any string: ");

    rev = getReverse(str);

    printf("Reversed string is: %s",rev);
    return 0;

char* getReverse(char str[]){

    static int i=0;
    static char rev[MAX];

         rev[i++] = *str;

    return rev;

Sample output:

Enter any string: mona
Reversed string is: anom

Sum of n numbers using recursion in c
Matrix multiplication using recursion in c
Multiplication using recursion in c
Lcm using recursion in c
Using recursion in c find the largest element in an array
Prime number program in c using recursion
Decimal to binary conversion in c using recursion
C program for fibonacci series using recursion
Reverse a string using recursion
Write a program for palindrome using recursion
Find factorial of a number using recursion in c program
Find gcd of a number using recursion in c program
Find sum of digits of a number using recursion using cprogram
Find power of a number using recursion using c program
Binary search through recurssion using c program
Reverse a number using recursion in c program
Big list of c program examples


Saran said...

Local character array cannot return from any function. That have function scope only...

Saran said...

Local character array cannot return from any function. That have function scope only..

Saran said...

Any how this is static variable. Take memory in data segment only, but unallocate memory cannot used and string reverse must done in same string only..

CProgrammer said...

how can I reverse a string including spaces with recursion or not?

Anonymous said...

Plz explain step wise, the working of function 'get Reverse'. I am a beginner

Binoj said...


void reverse(char *str, char *res)
static idx = 0;
if ( *str != '\0')
reverse(str+1, res);
res[idx++] = *str;


int main()
char str[100];
reverse("binoj", str);
printf("%s", str);

Suren K said...

char * rec_rev_str(char * str) {
if(*str != '\0'){
char * ret = (char *)malloc(strlen(str));
char * tmp = rec_rev_str(str+1);
strcpy(ret, tmp);
tmp = NULL;
strncat(ret, str, 1);
} else {
strncat(ret, str, 1);
return ret;
return NULL;

Anand Barnwal said...

A recusive way to reverse the same string:

void reverse(char str[], int l, int h)

if(l < h){
swap(&str[l], &str[h]);
reverse(str, l+1, h-1);

Suman Reddy said...

int main()
char str[50];
void rev_str(char *ptr);
printf("Reverse of string\n");
void rev_str(char *ptr)
char i;



purushothaman poovai said...

the simple method to reverse input using recursion:

char ch=getchar();



Recursion finished by ch have '\n',so the function return to calling function,After printing the value of ch.
so it print the last character in begin,and each characters are printed before each function returns.

Unknown said...

hi , this code suppose to be likeur last recursion example : #include
void print_reverse() {char c;
scanf("%c", &c); if (c == '\n') return;

int main() {
printf("Input a line\n");
printf("\n"); return 0; }
I still dont get\ understand something about the way recursion works, can u please clarify ?
1 when ur getting the ch by getchar or scanf the fun is getting 1 char each time ? where is it saved? or does it save a string ?
2 how\ what is the flow the reverse the string ? or is it printing 1 char each time from the end ? thanks

Ramesh Ramakrishnan said...
This comment has been removed by the author.