Code Snippets

  

C++ Source Code


Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 119,059 C++ Programmers for FREE! Ask your question and get quick answers from experts. There are 1,476 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!




Shortest Function to Reverse a String ( Uses Recursion )

Shows how a string can be reversed using Recursion.

Submitted By: born2c0de
Actions:
Rating:
Views: 34,800

Language: C++

Last Modified: March 2, 2008
Instructions: NOTE:
This code might be the shortest...but is not the best when it comes to performance.

Snippet


  1. /*
  2. Recursive Reverse String Algorithm
  3. The Shortest function to reverse a string.
  4. Written by: Sanchit Karve
  5.             born2c0de AT hotmail.com
  6. [born2c0de]
  7. */
  8.  
  9. #include <iostream>
  10.  
  11. using namespace std;
  12.  
  13. void ret_str(char* s)
  14. {
  15.     if(*s != '\0')
  16.          ret_str(s+1);
  17.  
  18.     cout<<*(s);
  19. }
  20.  
  21. int main()
  22. {
  23.    ret_str("born2c0de");
  24.    return 0;
  25. }

Copy & Paste


Comments


gbrao 2007-12-20 01:56:47

is there in the book c kernighan and Ritchie

born2c0de 2007-12-21 09:02:01

Is it? I had no idea. Thanks.

Tennison 2008-03-03 07:43:17

I got mine from the book C How to Program - Deitel

KevinGets 2008-07-19 12:52:12

/* This string reversal might be faster and more efficient -- uses minimal memory great for embedded systems BY: STEVE KING Tucson Arizona */ void reverse( char *ptr ) { char saved_char; // need to save off a character char *saved_ptr; // need to save off original ptr address saved_ptr = ptr; // save the actual ptr address // I only need to go through half the string to swap positions // so when the ratio of savedADDRESS to incrementedADDRESS is 2, then we quit while(strlen(saved_ptr)/strlen(ptr) != 2) { saved_char = *(saved_ptr+strlen(ptr)-1); // save off a char *(saved_ptr+strlen(ptr)-1) = *ptr; // swap chars *ptr = saved_char; // swap chars ptr++; // increment to next address } ptr = saved_ptr; // need to return the ptr address to original }

KevinGets 2008-07-19 12:54:25

Here's a nice one

KevinGets 2008-07-19 12:58:01

void reverse( char *ptr ) { /* STEVE KING TUCSON ARIZONA */ char saved_char; // need to save off a character char *saved_ptr; // need to save off original ptr address saved_ptr = ptr; // save the actual ptr address // I only need to go through half the string to swap positions // so when the ratio of savedADDRESS to incrementedADDRESS is 2, then we quit while(strlen(saved_ptr)/strlen(ptr) != 2) { saved_char = *(saved_ptr+strlen(ptr)-1); // save off a char *(saved_ptr+strlen(ptr)-1) = *ptr; // swap chars *ptr = saved_char; // swap chars ptr++; // increment to next address } ptr = saved_ptr; // need to return the ptr address to original }


Add comment


You must be registered and logged on to </dream.in.code> to leave comments.





Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month