0

Hello to everyone out there. Recently many people on different forums have been asking how to make a recursive function which reverses the string passed to it. But unlike the normal recrusive funtion which only prints out the reversed string, this implementation returns a pointer to reversed string.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/**
* The actual recursive algo which works behind the scenes
*
* @author ~s.o.s~
* @param src_string The pointer to constant character string which is to be used for reversing
* @param dest_string     The pointer to the string which will hold the reversed string
* @return a pointer to the reversed string
*/
char* _string_reverse( const char* src_string, char* dest_string )
{
     if( *src_string != '\0' )
    {
        *dest_string-- = *src_string++ ;
        _string_reverse( src_string, dest_string ) ;
    }

    return dest_string ;
}
/**
 * Acts as a simple interface which the main function or any function can call, hides the recursive nature of algo used.
 *The creation of the string to be returned occurs in this function which is then used by the recursive alg
 *
 * @author ~s.o.s~
 * @param my_string The const string to be reversed
 * @return Returns the reversed string
 */
char* string_reverse( const char* my_string )
{
    int length = strlen( my_string ) ;
    char* reversed = (char*) malloc( length + 1) ;
    reversed[ length ] = '\0' ;
    char* tmp = &( reversed[ 0 ] ) ;    /// preserve the original pos for returning
    reversed = &(reversed[ length - 1 ]) ;
    reversed = _string_reverse( my_string, reversed ) ;
    return tmp ;
}
/** The driver function which calls the core functionality
* @author ~s.o.s~
*/
int main( void )
{
    const char* my_string  = "Hello All !!" ;
    char* reversed = string_reverse( my_string );
    printf( "\n\nOriginal String: %s and Reversed String: {%s}", my_string, reversed ) ;

    return 0 ;
}
4
Contributors
6
Replies
9
Views
10 Years
Discussion Span
Last Post by roverphoenix
0

It is complex since rather than just printing out the reversed string, it "actually" returns a reversed string and that too using recursion.

Have a go at it urself and you will understand its not that simple to handle.

0

actually it looks complex bcoz you have initialized the array in read only memory it would be easier if you had dynamically allocated the array making it r+w

0

Here is a non-recursive version of the same.

#include <stdio.h>
#include <string.h>

char* _string_reverse( const char* src_string, char* dest_string )
{
	char *src_ptr = (char*)src_string + strlen(src_string)-1;
	char *dest_ptr = dest_string;
	
	while( src_ptr > src_string)
	{
		*dest_ptr++ = *src_ptr--;
	}
	*dest_ptr = 0;
	return dest_string;

}

int main()
{
	char inbuf[] = "Hello World";
	char outbuf[255] = {0};

	char *result = _string_reverse(inbuf,outbuf);
	printf("%s\n",result);

	return 0;
}
0

actually it looks complex bcoz you have initialized the array in read only memory it would be easier if you had dynamically allocated the array making it r+w

I dont know if I quite get you but are you saying that I should have created the input string dynamically and then modified the same ?

But thats not what I wanted to do, I wanted to keep the input string intact. Also creating two functions -- a interface and one actual functionality helps me get rid of using a static type qualifier.

0

by dynamically I meant doing a malloc and reading from stdin, and I mentioned read only memory since I thought you had used static char * earlier making the string read only, (thts the case with const char * as well), anyhow since thts not what you intended to do thts fine.

Be sure to adhere to our posting rules.