rageoutfool wrote: |
---|
Also in your main(), you wouldn't want to declare word as
an int. A word isn't an int, its an array of chars or a string. |
Correct. I just want to add that I'd prefer a string over a
char array, because I don't want to worry about size limits.
The loop suggestion is also a good one (that's how I would do it
too if there were no implementation constraints), however, the
assignment specifically asks to reverse the string using recursion.
I have a question.
What do startIndex and lastIndex represent?
What is ReverseText("WORLD", 1, 2) supposed
to be? "OWRLD"? "WROLD"? Something else?
Assuming that startIndex and lastIndex indicate the part of the string
that you want to reverse, I would do something that would work like this:
string ReverseTextFromTo(string word, int firstIndex, int lastIndex)
{
first, split word into three parts,
w1 (from the beginning of word to firstIndex),
w2 (from firstIndex to lastIndex),
w3 (from lastIndex to the end of word)
then, return w1 + ReverseText(w2) + w3
}
ReverseText("") = ""
ReverseText("abcd") =
ReverseText("bcd") + 'a' =
(ReverseText("cd") + 'b') + 'a' =
... =
"" + 'd' + 'c' + 'b' + 'a' =
"dcba" |
A couple of things I believe you'll find useful:
http://cplusplus.com/reference/string/string/length/
http://cplusplus.com/reference/string/string/substr/