Did you know that using a loop, you can examine a String one letter at a time?
The two key built-in String methods are length()
and charAt()
.
length()
returns an int
representing the total number of characters in the String
(including punctuation and whitespace). For example, if
the variable str
contains the String "hello"
, then str.length()
will
return 5
.
charAt( int n )
returns the
n
th character (char
)
in the String. The character positions are zero-based, so
if the variable str
contains the String "ligature"
, then str.charAt(0)
will return 'l'
, and str.charAt(4)
will return 't'
.
What is your message? A man, a plan, a canal: Panama! Your message is 31 characters long. The first character is at position 0 and is 'A'. The last character is at position 30 and is '!'. Here are all the characters, one at a time: 0 - 'A' 1 - ' ' 2 - 'm' 3 - 'a' 4 - 'n' 5 - ',' 6 - ' ' 7 - 'a' 8 - ' ' 9 - 'p' 10 - 'l' 11 - 'a' 12 - 'n' 13 - ',' 14 - ' ' 15 - 'a' 16 - ' ' 17 - 'c' 18 - 'a' 19 - 'n' 20 - 'a' 21 - 'l' 22 - ':' 23 - ' ' 24 - 'P' 25 - 'a' 26 - 'n' 27 - 'a' 28 - 'm' 29 - 'a' 30 - '!' Your message contains the letter 'a' 10 times. Isn't that interesting?
Assignments turned in without these things will receive half credit or less.
for
loop is defined so that it repeats as long as
i < message.length()
. Try changing it to <=
.
What happens? Answer in a comment, then change it back.
"box"
,
what is its length()
? What is the position of the last character
(the 'x'
)?
for
loop repeat as long as
i < message.length()
instead of i <= message.length()
?
a A e E i I o O u U
).
©2013 Graham Mitchell
This assignment is licensed under a
Creative Commons Attribution-NonCommercial-ShareAlike 3.0 United States License.