User Name Password Register
DaniWeb IT Discussion Community
All
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:
Oct 15th, 2006
Views: 3,900
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.
c Syntax | 5 stars
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. /**
  6. * The actual recursive algo which works behind the scenes
  7. *
  8. * @author ~s.o.s~
  9. * @param src_string The pointer to constant character string which is to be used for reversing
  10. * @param dest_string The pointer to the string which will hold the reversed string
  11. * @return a pointer to the reversed string
  12. */
  13. char* _string_reverse( const char* src_string, char* dest_string )
  14. {
  15. if( *src_string != '\0' )
  16. {
  17. *dest_string-- = *src_string++ ;
  18. _string_reverse( src_string, dest_string ) ;
  19. }
  20.  
  21. return dest_string ;
  22. }
  23. /**
  24.  * Acts as a simple interface which the main function or any function can call, hides the recursive nature of algo used.
  25.  *The creation of the string to be returned occurs in this function which is then used by the recursive alg
  26.  *
  27.  * @author ~s.o.s~
  28.  * @param my_string The const string to be reversed
  29.  * @return Returns the reversed string
  30.  */
  31. char* string_reverse( const char* my_string )
  32. {
  33. int length = strlen( my_string ) ;
  34. char* reversed = (char*) malloc( length + 1) ;
  35. reversed[ length ] = '\0' ;
  36. char* tmp = &( reversed[ 0 ] ) ; /// preserve the original pos for returning
  37. reversed = &(reversed[ length - 1 ]) ;
  38. reversed = _string_reverse( my_string, reversed ) ;
  39. return tmp ;
  40. }
  41. /** The driver function which calls the core functionality
  42. * @author ~s.o.s~
  43. */
  44. int main( void )
  45. {
  46. const char* my_string = "Hello All !!" ;
  47. char* reversed = string_reverse( my_string );
  48. printf( "\n\nOriginal String: %s and Reversed String: {%s}", my_string, reversed ) ;
  49.  
  50. return 0 ;
  51. }
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
Originally Posted by roverphoenix
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.
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
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.
bumsfeld | Posting Shark | Oct 15th, 2006
I can't believe it needs to be that complex!
Post Comment

Only community members can submit or comment on code snippets. You must register or log in to contribute.

DaniWeb Marketplace (Sponsored Links)
All times are GMT -4. The time now is 1:24 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC