Page 209 - Learn To Program With Scratch
P. 209

The program checks each letter in the input string one by one and
                          looks for vowels. Every time it finds a vowel, it increments a variable named
                          vowelCount by 1. The script uses a variable named pos (short for position) to
                          track the position of the character being checked. Let’s explore this script
                          in more detail.
                             First, the script asks the user to enter a sentence u. Scratch should
                          save the user’s string automatically in the built-in answer variable. Then it
                          sets vowelCount to 0 (since it hasn’t seen any vowels yet) and sets pos to 1 to
                          access the first letter of the input string.
                             Next, a repeat loop v checks every letter in the input string. The
                          length of operator reports the number of characters in the input string,
                          which is how many times the loop should repeat.
                             On each pass, the loop uses ch (short for character) to check one char-
                          acter of the input string w. In the first iteration of the loop, ch is set to the
                          first letter of answer. The second iteration sets ch to the second letter, and
                          so on, until the loop reaches the end of the string. The pos variable is used
                          to access the desired character.
                             The if block then checks whether the examined character is a vowel
                          x. If the character is a vowel, whether capital or lowercase, vowelCount is
                          increased by 1.
                             After checking one character, the loop increments pos by 1 y and starts
                          over to read the next character. When all the letters in the input string have
                          been checked, the loop terminates, and the program displays the number
                          of vowels it counted using the say block.
                             The techniques used in this example will be applied many times in the
                          rest of this chapter. Load the script VowelCount.sb2, run it several times, and
                          make sure you understand it thoroughly.

                          Comparing String Characters

            Palindrome .sb2  Our second example checks whether an integer entered by the user is a pal-
                          indrome. A palindrome is a number (or text string) that reads the same back-
                          ward and forward. For example, 1234321 and 1122332211 are palindromes.
                          Likewise, Racecar, Hannah, and Bob are a few text palindromes. To illus-
                          trate our palindrome-testing algorithm, let’s say that the input number is
                          12344321, as illustrated in Figure 8-3.

                                         pos1                          pos2







                                      1     2    3     4    4    3     2    1
                          letter position:  1  2  3    4    5    6     7    8

                          Figure 8-3: Using two variables to check whether or not a number is a palindrome



                                                                               String Processing   187

                                           www.it-ebooks.info
   204   205   206   207   208   209   210   211   212   213   214