|
-
May 16th, 2008, 02:19 PM
#1
Thread Starter
New Member
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
http://www.horstmann.com/bigcpp/solu...14/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;
}
-
May 16th, 2008, 03:36 PM
#2
Fanatic Member
Re: Help with recursion { recursive helper function }
We don't do homework. Try on your own and come back if you have more specific questions.
Never argue with fools, they will only drag you down to their level, and beat you with experience.
Q: How do you tell an experienced hacker from a novice?
A: The latter thinks there's 1000 bytes in a kilobyte, while the former is sure there's 1024 meters in a kilometer
-
May 18th, 2008, 07:55 AM
#3
Thread Starter
New Member
Re: Help with recursion { recursive helper function }
okay here is what I did is it the source okay ?
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;
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|