Thread: Recursion

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    60

    Recursion

    OK this is an easy program (since it's not too long or hard to understand) but since i want to use recursion to reverse a sentence around like "Hello!" turns into "!olleH", it makes it confusing. Here is my code:
    Code:
    #include <iostream>
    #include <string>
    #include <vector>
    
    using namespace std;
    
    class Sentence
    {
    public:
    	Sentence(string n);
    	string get_text();
    	void reverse();
    private:
    	string phrase;
    };
    
    Sentence::Sentence(string n)
    {
    	phrase = n;
    }
    string Sentence::get_text()
    {
    	return phrase;
    }
    void Sentence::reverse()
    {
    		char first = phrase[0];
    		char last = phrase[phrase.length() - 1];
    		if (first == last)
    		{
    		}
    		else
    		{
    			char temp = first;
    			first = last;
    			last = temp;
    			string Short = phrase.substr(1,last-1);
    			Sentence shorter_phrase(Short);
    			shorter_phrase.reverse();
    		}
    		
    	
    }
    
    
    int main()
    {
    	Sentence greeting("Hello!");
    	greeting.reverse();
    	cout<<greeting.get_text() <<"\n";
    	return 0;
    }
    Of course the problem is witht eh recursive function but i'm confused and i am having problems following the code since ti doesn't flip it around. I can follow the logic but i can't seem to find the problem. If anyone can give me suggestions about fixing it would be appreciated. Thank you for you time.
    C++ can hurt.

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Couple of issues: (a) I think you want to use pointers for "first" and "last" and (b) substr() returns a copy of the substring - so once the substring is reversed, you'll have to put back into phrase.

    gg

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    60
    hmm i'm still having some problems with the recursion. Does anyone have any other suggestions?
    C++ can hurt.

  4. #4
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Your sentence 'short' is only a copy of the substring, not the actual substring itself. So if the original word is "Hello", then it reverses the first and last to "!elloH", then 'short' equals "ello" which gets swapped in the next recursion loop, but since it's just a copy and not part of the original string, your original sentence still will say "!elloH". You're probably better off doing something like:
    Code:
    void Sentence::reverse(int beg, int end)
    {
       if (beg>=end)
          return;
       else
          //swap beg and end letter.
          //reverse(beg+1, end-1);
    }
    Last edited by PJYelton; 03-05-2003 at 03:55 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Template Recursion Pickle
    By SevenThunders in forum C++ Programming
    Replies: 20
    Last Post: 02-05-2009, 09:45 PM
  2. Recursion... why?
    By swgh in forum C++ Programming
    Replies: 4
    Last Post: 06-09-2008, 09:37 AM
  3. a simple recursion question
    By tetra in forum C++ Programming
    Replies: 6
    Last Post: 10-27-2002, 10:56 AM
  4. To Recur(sion) or to Iterate?That is the question
    By jasrajva in forum C Programming
    Replies: 4
    Last Post: 11-07-2001, 09:24 AM
  5. selection sorting using going-down and going-up recursion
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 11-02-2001, 02:29 PM