|
||||||
| 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. |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
|
|||
|
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;
}
|
| Sponsored Links |
|
|
|
|||||
|
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 |
|
|||
|
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;
}
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
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 |
| John | ........ | 223.00000 |
| dargueta | ........ | 168.00000 |
| Xav | ........ | 164.00000 |
| gaylo565 | ........ | 18.00000 |
| WingedPanther | ........ | 15.00000 |
| |pH| | ........ | 15.00000 |
| Johnnyboy | ........ | 3.00000 |
| navghost | ........ | 1.00000 |
Goal: 100,000 Posts
Complete: 65%