go to previous page   go to home page   go to next page

Answer:

Yes.

  1. If the problem is easy, solve it immediately. — an empty string has length 0
  2. If the problem can't be solved immediately, divide it into easier problems — divide the string into a first character and a tail.
    • Solve the easier problems. — the length of the string is 1 + length of the tail.

Bubbles

ant

The picture shows how this works with the string "ant".

The length of "ant" is
1 + (the length of "nt")
1 + (1 + the length of "t")
1 + (1 + (1 + the length of ""))
= 1 + (1 + (1 + 0))
= 1 + (1 + (1))
= 1 + 2
= 3

Sometimes recursion seems backwards. The last "1" added to the sum is for the first character of the string. Look at the bottom bubble of the diagram. If you start with it and go upwards, it is like counting characters 0, 1, 2, 3 from the end of the string to the first character.


QUESTION 5:

(Review: ) Is the empty string an object?


go to previous page   go to home page   go to next page