Results 1 to 7 of 7

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date: Dec:2006
    Location: SOFIA
    Posts: 18

    recursion { recursive helper function }

    source code za C++/G++ linux dev C++ windows



    hint Exercise P14.1 Exercise P14.1

    http://www.horstmann.com/bigcpp/solu...14/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;
    }

  2. #2
    XaMaB's Avatar
    Join Date: Nov:2001
    Location:
    Posts: 20,387
    : XaMaB; . 0.42

    In God we Trust (all others must submit a X.509 certificate). , ()

  3. #3
    Registered User
    Join Date: Dec:2006
    Location: SOFIA
    Posts: 18

    Unhappy

    Quote Originally Posted by XaMaB View Post

  4. #4

  5. #5
    Registered User
    Join Date: Dec:2006
    Location: SOFIA
    Posts: 18
    Quote Originally Posted by MFrost View Post
    , .
    , , .
    "helper function that reverses a substring of the message text "



    ??? substring of the message text ???

  6. #6
    XaMaB's Avatar
    Join Date: Nov:2001
    Location:
    Posts: 20,387
    / . , ,
    : XaMaB; . 0.42

    In God we Trust (all others must submit a X.509 certificate). , ()

  7. #7
    Registered User
    Join Date: Dec:2006
    Location: SOFIA
    Posts: 18


    Code:
    #include <string>
    #include <iostream>
    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();
       string reverseSubstring(int beginning,int end);
         
    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;
    }
    string Sentence::reverseSubstring(int beginning,int end)
    {
       if (phrase != "")
       {
          string sBegining = phrase.substr(0, beginning - 1);
          string sEnd = phrase.substr(end,phrase.length()-1);
          Sentence toReverse(phrase.substr(beginning -1,end - 1));
          toReverse.reverse();
          phrase = sBegining + toReverse.phrase + sEnd;
       }
       return phrase;
    }
    
    int main()
    {
       Sentence greeting("Hello!");
       //cout << greeting.reverse() << "\n";
       cout << greeting.reverseSubstring(2,4) << "\n";
       return 0;
    }
    Last edited by mibit; 18th May 2008 at 15:24.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Copyright © 1999-2011 . .
iskamPC.com | mobility.BG | Bloody's Techblog | | 3D Vision Blog |