We need you!

We're working hard on the next version of Developer Fusion. Let us know what you think we should be up to!

Members

Technology Zones

Articles

Hosted By

MaximumASP

Info

Help with recursion { recursive helper function }

Last post 05-16-2008 9:04 PM by mibit. 0 replies.
Page 1 of 1 (1 items)
Sort Posts: Previous Next
  • 05-16-2008 9:04 PM

    • mibit
    • Not Ranked
    • Joined on 05-16-2008
    • Bulgaria
    • New Member
    • Points 5

    Help with recursion { recursive helper function }



    Can you please help me with the source code  solving this task under   C++/G++ (linux) or dev C++ (windows)



    Below you can see a hint for solving Exercise P14.1     but I need to write the source for Exercise P14.1 Please help me

    http://www.horstmann.com/bigcpp/solutions/ch14/ExP14_1.cpp

    #include <string>

    using namespace std;

    /**
       Reverse a sentence.
    */
    class Sentence
    {
    public:
       /**
          Creates a Sentence object.
          @param aPhrase a sentence to reverse.
       */
       Sentence(string aPhrase);
      
       /**
          Reverses this sentence.
          @return the reversed sentence
       */
       string reverse();
        
    private:
       string phrase;
    };

    Sentence::Sentence(string aPhrase)  
    {
       phrase = aPhrase;
    }

     
    string Sentence::reverse()
    {
       if (phrase != "")
       {
          string c = phrase.substr(0, 1);
          string rest = phrase.substr(1, phrase.length() - 1);
          Sentence tailSentence(rest);
          phrase = tailSentence.reverse() + c;
       }
       return phrase;
    }

    int main()
    {
       Sentence greeting("Hello!");
       cout << greeting.reverse() << "\n";
       return 0;
    }



     

    • Post Points: 5
Page 1 of 1 (1 items)