0

I am attempting to return a reversed version of the string "Loyce" in an iterative function. The program runs, but it crashes after running. Any help would be appreciated.

string Iterative4(string word)
{
    for(int x = 0; x < word.length(); x++)
    {
        if (word.length() - 1 > 0)
        {
            char last_char = word[word.length() - 1];
            word.erase(word.length() - 1);
            string go = Iterative4(word);
            return go;
        }
    }

}

int main() {
    cout << Iterative4("Loyce") << endl;
}
4
  • Do you know about std::reverse? Commented Oct 15, 2014 at 0:09
  • 2
    Your Iterative4 appears to be both iterative and recursive. I'm guessing that's not really what you want. Commented Oct 15, 2014 at 0:10
  • @JonathanPotter To me, it looks like a homework assignment, where this is the "fourth implementation" of the same problem (reversing a string) in an iterative way. If this is true, std::reverse is of course no option (or maybe to be used in one of the implementations they should do). Commented Oct 15, 2014 at 0:11
  • I see a few problems: First, what is the outer loop for if you never use x? You call your function Iterative4 recursively, so, probably, you solved problem recursively before and that is an artifact of your changes? Do something with x instead. Second, you erase the last character from word, then you don't do anything with it. It will be lost. Since you want to reverse the string, I guess it's not too difficult for you to guess where you should put it ;-) Commented Oct 15, 2014 at 0:16

3 Answers 3

1

Use the at function of string. Do not use recursive and iterative together.

string Iterative4 ( const string& word )
{
    std::string l_bla;
    bla.reserve(word.size());
    for ( string::size_type x = word.length ( ); x > 0; x-- )
    {

        l_bla += word.at ( x -1 );

    }
    return l_bla;
}

this should work, but i didnt test it, maybe you have to change something small

EDIT: Tested now and it works perfectly

Sign up to request clarification or add additional context in comments.

7 Comments

Pass word by const reference.
Reserve l_bla to word's size.
Well i think thats a unnecesarry improvement @ this point, but year, i can do it
It's actually a better improvement than the first one.
It looks correct to me. The only remaining issue is if the string size is too big for int, but we can live with it.
|
0

One immediate problem is like that if word.length() - 1 is not greater than 0, then the function Iterative4 fails to return a value. Since the function is declared to return a string, this results in undefined behavior, which may include a crash.

Comments

0
string Iterative4(string word)
{
    if (word.length() - 1 > 0)
    {
        char last_char = word[word.length() - 1];
        word.erase(word.length() - 1);
        string go = Iterative4(word);
        return go;
    }
    else
        return word;
}

int main() {
    cout << Iterative4("Loyce") << endl;
}

I didn't test it.

If you insist on recursion, these or sth like these should work.

As a homework, it's just fine.

But if you are to do some real work, it should be noticed that recursion is a very very expensive action.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.