Tuesday, April 17, 2012

Challenge 060’s Solution

In Challenge 60, inserting a word such as “Phoenix” into the method will create an output of “xineohP”. This is mainly due to the use of the substring command. The printString method starts by accepting a String value and assigning it to the variable s. It then gets s and performs the substring method with a parameter of 1. This means that it gets s and removes the first character from the (first) word. This altered s is then resent to the printString method via recursion, and the process is repeated until s has zero elements. At that point, the s containing one element is sent through the substring method again, but this time with the parameters 0 and 1. This tells it to print the first character in s, starting at element 0, which, in this case, is the last letter of the original String s. Finally, recursion causes this procedure to repeat until the original s is sent through the last substring method, which, in effect, prints s backwards.
 
−Phoenix

3 comments:

  1. In this challenge, what is the base case for the recursive method?

    ReplyDelete
  2. A base case is a parameter value that, when used, doesn’t call upon recursion. The base case for this method would be an empty “String” value for “s”. If one were to simply input "", the “printString” method would never be called because the “if” statement would never run.

    −Phoenix

    ReplyDelete