•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 397,698 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,521 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser:
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.
Last edited : Oct 15th, 2006.
#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 ; }
Comments (Newest First)
roverphoenix | Newbie Poster | Nov 17th, 2006
•
•
•
•
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.
~s.o.s~ | Rebellion Revamped | Nov 15th, 2006
Ancient Dragon | Most Valuable Poster | Nov 13th, 2006
•
•
•
•
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;
}
roverphoenix | Newbie Poster | Oct 30th, 2006
•
•
•
•
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
~s.o.s~ | Rebellion Revamped | Oct 18th, 2006
bumsfeld | Posting Shark | Oct 15th, 2006
•
•
•
•
I can't believe it needs to be that complex!
Post Comment
•
•
•
•
DaniWeb Marketplace (Sponsored Links)
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.