Code Snippets

  

C++ Source Code


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

Join 86,466 C++ Programmers. There are 1,281 online right now! Ask your question and get quick answers from Dream.In.Code experts. Join the #1 programming help community on the internet! Registration is fast and FREE... Join Now!

Chat LIVE With a C++ Expert
Powered by LivePerson.com

Register to Make This Box Go Away!


Shortest Function to Reverse a String ( Uses Recursion )

Shows how a string can be reversed using Recursion.

Submitted By: born2c0de
Actions:
Rating:
Views: 26,262

Language: C++

Last Modified: December 27, 2005
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


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