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

[img]http://mail.dir.bg/~storage/cPlus_plus111.jpg[/img]

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;
}

Recommended Answers

All 3 Replies

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.

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.

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 : - )

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.