Results 1 to 5 of 5
  1. #1
    OptimusPrime is offline Member
    Join Date
    Dec 2009
    Posts
    14
    Rep Power
    0

    Default recursion and call stack problem

    i have this code,

    static String reverse(String s) {
    if (s.length() <= 1) {
    return s;
    }
    return reverse(s.substring(1)) + s.charAt(0);
    }


    and these two questions,
    1.Explain the algorithm.

    2.Rewrite the recursion and implement it iteratively. Use the same method signature, but call the method reverseIterative. Use a while-loop in your implementation, and implement the same approach you answered in the previous question.

    im new to recursions,
    can anyone help and explain?
    thanks.
    OP

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    Thinking recursively is thinking lazy (I'm very good at it ;-) Reversing an empty String or a String with just one character in it is easy: simply do nothing; otherwise the String can be represented as fT where f is the first single character and T represents the rest of the String; let something/somebody (else) do the reversion of T and I append the single character f to it; effectively that reverses the entire String. (try it on paper).

    You do the coding because it's your assignment.

    kind regards,

    Jos
    Last edited by JosAH; 12-26-2009 at 02:49 PM.

  3. #3
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    8

    Default

    Hi OptimusPrime

    I've attached a PDF document explaining this recursive algorithm in detail. This should help you find an iterative algorithm for your assignment. Plagiarize at own loss. Please mind the typos.

    Good luck
    Tim
    Attached Files Attached Files
    Last edited by tim; 12-26-2009 at 07:48 PM.
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

  4. #4
    OptimusPrime is offline Member
    Join Date
    Dec 2009
    Posts
    14
    Rep Power
    0

    Default

    thanks alot!
    it helped!
    :):):)

  5. #5
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    8

    Default

    Glad to help OptimusPrime.

    Good luck with your assignments and studying. ;)
    Tim
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

Similar Threads

  1. Java Recursion Problem
    By gmnnn in forum Threads and Synchronization
    Replies: 1
    Last Post: 12-06-2009, 05:22 PM
  2. Stack problem..pls help
    By Mika in forum New To Java
    Replies: 1
    Last Post: 02-16-2009, 09:10 AM
  3. Problem in Calculator implementation using Stack
    By realahmed8 in forum New To Java
    Replies: 1
    Last Post: 12-20-2008, 12:58 AM
  4. Replies: 1
    Last Post: 11-17-2008, 10:32 AM
  5. Help with recursion
    By scts102 in forum New To Java
    Replies: 1
    Last Post: 11-19-2007, 11:07 PM

Posting Permissions

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