Lost Password?

Go Back   CodeCall Programming Forum > Software Development > C and C++

Vote on your favorite hash algorithm here!

C and C++ C and C++ forum for discussing all forms of C except for C#. These languages are powerful low level languages used for creating Operating Systems, Device Drivers, compilers and much more.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-16-2008, 01:40 PM
mibit mibit is offline
Newbie
 
Join Date: May 2008
Posts: 3
Credits: 0
Rep Power: 0
mibit is on a distinguished road
Default 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

horstmann.com/bigcpp/solutions/ch14/ExP14_1.cpp
Code:
#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;
}
Attached Thumbnails
help-recursion-recursive-helper-function-cplus_plus111.jpg  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 05-16-2008, 01:40 PM
mibit mibit is offline
Newbie
 
Join Date: May 2008
Posts: 3
Credits: 0
Rep Power: 0
mibit is on a distinguished road
Default Re: Help with recursion { recursive helper function }

thanks in advance
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 05-17-2008, 05:13 PM
WingedPanther's Avatar   
WingedPanther WingedPanther is offline
Super Moderator
 
Join Date: Jul 2006
Age: 35
Posts: 2,530
Last Blog:
wxWidgets is NOT code ...
Credits: 911
Rep Power: 28
WingedPanther is a jewel in the roughWingedPanther is a jewel in the roughWingedPanther is a jewel in the roughWingedPanther is a jewel in the rough
Default Re: Help with recursion { recursive helper function }

It looks like you already have the code there... Perhaps I'm missing something.
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum
Chat with other CodeCall members on IRC; connect to irc.codecall.net and join #codecall
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 05-18-2008, 07:37 AM
mibit mibit is offline
Newbie
 
Join Date: May 2008
Posts: 3
Credits: 0
Rep Power: 0
mibit is on a distinguished road
Default Re: Help with recursion { recursive helper function }

See what I did Should I change anything ?
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;
}
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Inline function Chinmoy C Tutorials 1 04-07-2008 03:13 PM
using Template function Chinmoy C Tutorials 4 04-03-2008 04:16 AM
function pointer Chinmoy C Tutorials 0 03-19-2008 12:52 AM
recursion Chinmoy C Tutorials 0 03-18-2008 11:57 PM


All times are GMT -5. The time now is 06:47 PM.

Contest Stats

Xav ........ 1359.44
MeTh0Dz|Reb0rn ........ 1072.63
WingedPanther ........ 911.18
marwex89 ........ 906.86
morefood2001 ........ 899.18
John ........ 887.37
Brandon W ........ 770.65
chili5 ........ 312.39
Steve.L ........ 264.99
dcs ........ 232.34

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 83%

Ads