Page 208 - Learn To Program With Scratch
P. 208

revisiting the String data type
                         As I mentioned in Chapter 5, Scratch has three data types: Boolean, number,
                         and string. At its simplest, a string is just an ordered sequence of characters.
                         These characters can include letters (both upper- and lowercase), digits,
                         and other symbols that you can type on your keyboard (+, -, &, @, and so
                         on). You can use strings in your programs to store names, addresses, phone
                         numbers, book titles, and more.
                             In Scratch, the characters of a string are stored sequentially. For
                         e xample, if you have a variable called name, executing the command set
                         name to Karen would store the characters as illustrated in Figure 8-1.

                                                                           Reports the letter at the
                                                                           specified position in a
                                                                           string.
                               K    a     r    e     n                     Reports the number of
                          letter  1  2    3    4     5  of name            letters in a string.

                         Figure 8-1: A string is stored as a sequence of characters .

                             You can access individual characters of a string with the letter of opera-
                         tor. For example, the block letter 1 of name returns the letter K, and letter
                         5 of name returns the letter n. Scratch also provides the length of operator,
                         which returns the number of characters in a string. If you use these two
                         operators with repeat blocks, you can count characters, examine multiple
                         characters, and do many other useful things, as I’ll demonstrate in the fol-
                         lowing subsections.

                         Counting Special Characters in a String
           VowelCount .sb2  Our first example script, shown in Figure 8-2, counts how many vowels are
                         in an input string. It asks the user to enter a string, and then it counts and
                         displays the number of vowels in that string.




                                                           Get the user’s answer. Initialize vowelCount to
                                                          0 and set pos to 1 to prepare for accessing
                                                           the first letter of the input string.
                                                          Loop to check every letter in the input string.
                                                          Set ch to the next letter of answer.




                                                          If ch is a vowel, increment vowelCount.
                                                          Prepare to access next character.




                         Figure 8-2: Vowel-counting program


           186   Chapter 8
                                           www.it-ebooks.info
   203   204   205   206   207   208   209   210   211   212   213