Problem with recursion

Discussion in 'C++' started by Gaijinco, Mar 26, 2006.

  1. Gaijinco

    Gaijinco Guest

    I'm trying to do a function that returns a string which is a string the
    characters in inverse order of the one given as a parameter. It
    compiles but it crashes! Can anyone help me?

    string inverse(string s, int pos, int size)
    {
    if(pos==size-1)
    return s;
    else
    {
    int idx = s.size()-1;
    char character = s.at(idx);
    s.erase(idx);
    s.insert(s.begin()+pos,character);
    inverse(s,pos+1,size);
    }
    }
     
    Gaijinco, Mar 26, 2006
    #1
    1. Advertising

  2. Gaijinco wrote:
    > I'm trying to do a function that returns a string which is a string the
    > characters in inverse order of the one given as a parameter. It
    > compiles but it crashes! Can anyone help me?
    >
    > string inverse(string s, int pos, int size)
    > {
    > if(pos==size-1)


    Better:

    if(pos >= size)
    return s;

    > return s;
    > else
    > {
    > int idx = s.size()-1;
    > char character = s.at(idx);
    > s.erase(idx);
    > s.insert(s.begin()+pos,character);
    > inverse(s,pos+1,size);


    This probably should be:

    inverse(s, pos + 1, size - 2);

    > }
    > }
     
    Maxim Yegorushkin, Mar 26, 2006
    #2
    1. Advertising

  3. Gaijinco

    Guest

    Gaijinco wrote:
    > I'm trying to do a function that returns a string which is a string the
    > characters in inverse order of the one given as a parameter. It
    > compiles but it crashes! Can anyone help me?
    >
    > string inverse(string s, int pos, int size)


    Your implementation make far too many copies, and the contract is
    unclear. If I had to guess, I'd say it returns a string which is a copy
    of the characters s[pos]..s[pos+size] reversed. Is this what you mean?
     
    , Mar 27, 2006
    #3
  4. Gaijinco

    Gaijinco Guest

    Yeah, if you input carlos it would output solrac but there is something
    quite odd, this works fine:

    void inverse(string s, int pos, int size)
    {
    if(pos==size-1)
    {
    cout << s;
    return s;
    }
    else
    {
    int idx = s.size()-1;
    char character = s.at(idx);
    s.erase(idx);
    s.insert(s.begin()+pos,character);
    inverse(s,pos+1,size);
    }
    }

    But if I try to return the string, then the program crashes. I'm not
    sure why is that!
     
    Gaijinco, Mar 27, 2006
    #4
  5. Gaijinco

    Howard Guest

    "Gaijinco" <> wrote in message
    news:...
    > Yeah, if you input carlos it would output solrac but there is something
    > quite odd, this works fine:
    >
    > void inverse(string s, int pos, int size)
    > {
    > if(pos==size-1)
    > {
    > cout << s;
    > return s;
    > }
    > else
    > {
    > int idx = s.size()-1;
    > char character = s.at(idx);
    > s.erase(idx);
    > s.insert(s.begin()+pos,character);
    > inverse(s,pos+1,size);
    > }
    > }
    >
    > But if I try to return the string, then the program crashes. I'm not
    > sure why is that!
    >


    You defined the function as void, but then have a "return s" statement in
    the first part.

    If it is supposed to return a string, then define it to return a string.
    And then you also need a return statement for the else condition. (Probably
    "return inverse(s,pos+1,size):"? I haven't tested the thought.)

    If it is not supposed to return a string, then it should be operating on s
    directly, and never returning a value. In that case, make s a string&
    instead, and change "return s;" to just "return;".

    -Howard
     
    Howard, Mar 28, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. kelvSYC

    Recursion-related problem

    kelvSYC, Nov 18, 2003, in forum: Java
    Replies:
    2
    Views:
    334
    Eric Sosman
    Nov 18, 2003
  2. Anakin
    Replies:
    14
    Views:
    761
    Thomas G. Marshall
    Apr 13, 2005
  3. JimC
    Replies:
    3
    Views:
    535
  4. Allan W
    Replies:
    4
    Views:
    551
    Jos A. Horsmeier
    Jan 22, 2004
  5. Replies:
    8
    Views:
    798
    John Reye
    Apr 26, 2012
Loading...

Share This Page