Search in shivacherukuri.tech@blogger.com

Monday, June 14, 2010

reverse of string without using extra memory or swaping

http://www.allinterview.com/showanswers/16431.html

 

 

/* Following code does as intended */

#include <stdio.h>

 

#define REVERSE_STRING(X) Rstring(X, *(X), strlen(X)-1)

 

void Rstring( char *str, char c, int index )

{

        if( index != 0 )

               Rstring( str, *(str+(strlen(str))-index),

index-1);     

        *(str+index) = c;             

}

 

int main( void )

{

        char str[] = "Dharmendra Patel";

        printf("Actual string is [%s]\n", str);

        REVERSE_STRING(str);

        printf("Reversed string is [%s]\n", str);

        return 0;

}