Help with recursion { recursive helper function }

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: May 2008
Posts: 7
Reputation: mibit has a little shameless behaviour in the past 
Solved Threads: 0
mibit mibit is offline Offline
Newbie Poster

Help with recursion { recursive helper function }

 
0
  #1
May 16th, 2008
Can you please help me with the source code solving this task under C++/G++ (linux) or dev C++ (windows)

http://mail.dir.bg/~storage/cPlus_plus111.jpg

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

  1.  
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. /**
  7.   Reverse a sentence.
  8. */
  9. class Sentence
  10. {
  11. public:
  12. /**
  13.   Creates a Sentence object.
  14.   @param aPhrase a sentence to reverse.
  15.   */
  16. Sentence(string aPhrase);
  17.  
  18. /**
  19.   Reverses this sentence.
  20.   @return the reversed sentence
  21.   */
  22. string reverse();
  23.  
  24. private:
  25. string phrase;
  26. };
  27.  
  28. Sentence::Sentence(string aPhrase)
  29. {
  30. phrase = aPhrase;
  31. }
  32.  
  33.  
  34. string Sentence::reverse()
  35. {
  36. if (phrase != "")
  37. {
  38. string c = phrase.substr(0, 1);
  39. string rest = phrase.substr(1, phrase.length() - 1);
  40. Sentence tailSentence(rest);
  41. phrase = tailSentence.reverse() + c;
  42. }
  43. return phrase;
  44. }
  45.  
  46. int main()
  47. {
  48. Sentence greeting("Hello!");
  49. cout << greeting.reverse() << "\n";
  50. return 0;
  51. }
Attached Thumbnails
cPlus_plus111.jpg  
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 4
Reputation: mynameisor is an unknown quantity at this point 
Solved Threads: 1
mynameisor mynameisor is offline Offline
Newbie Poster

Re: Help with recursion { recursive helper function }

 
0
  #2
Dec 8th, 2008
There are a few errors.
1. In ur constructor u will have to use
strcpy(phrase,aphrase);
2. then the header file for cout is not included
This shud solve ur problem.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 392
Reputation: StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light 
Solved Threads: 72
StuXYZ StuXYZ is offline Offline
Posting Whiz

Re: Help with recursion { recursive helper function }

 
0
  #3
Dec 8th, 2008
Mynameisor is incorrect about point 1. But this is clean code that you have copied from the answer book. With your current pathetic knowledge HOW do you think you are going to get away with submitting it??

Where is you EFFORT? Not immediately obvious, so my effort is suddenly lacking.
experience is the most expensive way to learn anything
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 91
Reputation: brechtjah is an unknown quantity at this point 
Solved Threads: 9
brechtjah's Avatar
brechtjah brechtjah is offline Offline
Junior Poster in Training

Re: Help with recursion { recursive helper function }

 
0
  #4
Dec 8th, 2008
To help you out a bit more;
To use cout you need to have iostream included

#include <iostream>

Also, don't forget to use
system("pause");
because otherwise your console window will close immediatly and you won't be able to see your code running : - )
Last edited by brechtjah; Dec 8th, 2008 at 7:24 pm.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC