A string that contains no characters has length 0.
Let us write a method that computes the length of a string.
The Java class String
includes a method length()
so it seems odd to write such a method.
But string length is a really good example of recursion.
Pretend that the people who wrote String
forgot to include a length()
method,
so now you are stuck with the job.
Recall that a String
uses double quotes.
"x"
is a String
of length one.
A char
constant uses single quotes, 'x'
.
What is the length of each of the strings in the table?
Say that you know that the length of
uperfragilisticexpialadocious
is 29. What is the length of
Superfragilisticexpialadocious