reverse a string using recursion in c language | CTechnotips


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

0 comments:

Post a Comment