reversing a string recursively

This is a discussion on reversing a string recursively within the C++ Programming forums, part of the General Programming Boards category; Be gentle I am new at this but I need some help. I have to create a function and then ...

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    21

    reversing a string recursively

    Be gentle I am new at this but I need some help. I have to create a function and then use it recursively so that it reverses the string. This is my program so far..

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    //PROTOTYPE
    
    string Reverse(string s, int m);   //reverse s from
                   //character at m-th position out to the end
    
    // MAIN PROGRAM
    
    int main () 
    {
      string s = "GeorgeWBush";
      
      cout << Reverse(s,0) << endl;
    
      return 0;    
    }
    
    //IMPLEMENTATION OF PROTOTYPE
    
    string Reverse(string s, int m)
    {
    	string t = "";  //null string	
    	if (m == s.length()-1)
    	{
    		return t + s[s.length()-1];	  
    	} else
    		return Reverse (s, m+1);
    }
    My dilema is that all it prints is the 'h', I know that there is more code to this but I am at a loss...any helpful hints or suggestions??

  2. #2
    Peace
    Join Date
    Aug 2001
    Posts
    1,510
    [Mod Duties] Code tags added to make code more readable, and indentation altered. [/Mod Duties]
    "There's always another way"
    -lightatdawn (lightatdawn.cprogramming.com)

  3. #3
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    I don't like the string class... but I forumlated this using regular character arrays; and it works.

    Code:
    #include <iostream>
    #include <string>
    
    
    void Reverse(char *s, int m)
    {
    	using namespace std;
    	if (m != strlen(s))
    	{
    		cout << s[strlen(s)-m-1)];
    		Reverse(s, m+1);
    	}
    }
    
    
    int main(int argc, char* argv[])
    {  
    	using namespace std;
    	char *ms = "GeorgeWBush";
    	Reverse(ms,0);
    	cout << endl;
    	return 0;
    }

  4. #4
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Pass in a string reference.

    Kuphryn

  5. #5
    Registered User
    Join Date
    Feb 2002
    Posts
    21

    thank you!

    Thank you for your help! I appreciate it!!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  3. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  4. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  5. Reversing a String
    By ToasterPas in forum C++ Programming
    Replies: 10
    Last Post: 08-14-2001, 12:20 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21