reverse a string
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: reverse a string

  1. #1
    Join Date
    Apr 2009
    Posts
    1

    reverse a string

    hi , i need to implement the method public String reverse(string string) ...im only allowed to use the methods concant, length, and charAt from the String object and i have to use recursion( no for loops)
    I know how to to this with the substring method but in this case i cant use it

  2. #2
    Join Date
    Dec 2005
    Location
    England
    Posts
    86

    Re: reverse a string

    This sounds like a homework problem. Based on the methods provided, I think the general idea you're expected to use is to take the first character of the string and call reverse on the rest, then concatenate the reversed rest of the string with the first character (on the end, of course). Obviously, your base case (which you must check before the rest of the method) is that the reverse of an empty or single-character string (where str.length() < 2) is itself.

    Note that we wouldn't do this in the real world: Java is not good with recursion, and this wouldn't work well for long strings.

  3. #3
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: reverse a string

    Quote Originally Posted by Twey View Post
    Note that we wouldn't do this in the real world: Java is not good with recursion, and this wouldn't work well for long strings.
    In the real world, you'd probably put it in a StringBuilder, call reverse() on it, and pull it out again...

    Controlling complexity is the essence of computer programming...
    B. Kernighan
    Please use &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  4. #4
    Join Date
    Dec 2005
    Location
    England
    Posts
    86

    Re: reverse a string

    Well, that works fine if the string fits comfortably into memory a few times, yes

  5. #5
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: reverse a string

    Is recursion with String going to use less memory? - every call copies the string (on concatenation), whereas reverse in StringBuilder does it all in place...

    And simple truth miscalled simplicity...
    W. Shakespeare
    Last edited by dlorde; April 7th, 2009 at 05:23 PM.
    Please use &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  6. #6
    Join Date
    Dec 2005
    Location
    England
    Posts
    86

    Re: reverse a string

    No, it's not. I was just pointing out that the StringBuilder technique will not work in all real-world situations, either (although of course it's much better for many). For extremely large strings, you'd want a stack (and the string in a queue to start with), unless the string wouldn't fit in memory even once, in which case you'd need to use a stream to some other storage medium that could be read backwards.

    Of course, this is where lazy evaluation comes into its own the recursive function actually works quite well if evaluated lazily I don't think that's really possible in Java, though, alas.
    Last edited by Twey; April 8th, 2009 at 08:10 AM.

  7. #7
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: reverse a string

    OIC. Fair enough

    Let us change our traditional attitude to the construction of programs. Instead of imagining that our main task is to instruct a computer what to do, let us concentrate rather on explaining to human beings what we want a computer to do...
    D. Knuth
    Please use &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center