Reverse a string using recursion in c





C code to reverse a string by recursion:

#include<stdio.h>
#define MAX 100
char* getReverse(char[]);

int main(){

    char str[MAX],*rev;

    printf("Enter  any string: ");
    scanf("%s",str);

    rev = getReverse(str);

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

char* getReverse(char str[]){

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

    if(*str){
         getReverse(str+1);
         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

12 comments:

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
thanx

Binoj said...

#include

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

}
return;
}


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);
if(tmp){
strcpy(ret, tmp);
free(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...

#include
int main()
{
char str[50];
void rev_str(char *ptr);
fgets(str,50,stdin);
printf("Reverse of string\n");
rev_str(str);
}
void rev_str(char *ptr)
{
char i;
if(*ptr)
{
i=*ptr;
ptr++;
rev_str(ptr);
printf("%c",i);

}

}

purushothaman poovai said...

the simple method to reverse input using recursion:

main()
{
char ch=getchar();

if(ch!='\n')
main();

printf("%c",ch);
}

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;
print_reverse();
putchar(c);}

int main() {
printf("Input a line\n");
print_reverse();
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.