Page 211 - Learn To Program With Scratch
P. 211

In the next section, we’ll explore some of the most common operations
                          on strings and see some strategies for writing string manipulation proce-
                          dures in Scratch.



               String manipulation examples
                          The letter of operator only lets you read the individual characters of a
                          string. If you want to insert characters into (or remove characters from)
                          a string, you have to do all the work yourself.
                             In Scratch, you can’t alter the characters in a string, so the only way to
                          change a string is to create a new one. For example, if you want to capital-
                          ize the first letter in the string “jack”, you need to create a new string that
                          contains the letter J followed by the rest of the letters, ack. The idea is to use
                          the letter of operator to read the letters of the original string and append
                          theses letters to the new string, as needed, using the join operator.
                             In this section, we’ll develop some simple applications that demonstrate
                          common string manipulation techniques.

                          Igpay Atinlay

               PigLatin .sb2  What if our sprites could speak a secret language? In this section, we’ll
                          teach them a coded language called pig latin. Our rules for creating pig
                          latin words will be simple. To convert a word into pig latin, move the first
                          letter to the end and add the letters ay. So, the word talk becomes alktay, fun
                          becomes unfay, and so on. Now that you know the rules, can you tell what
                          the title of this section originally said?
                             The strategy we’ll use to convert a word into pig latin is illustrated in
                          Figure 8-5, using the word scratch.


                          Input  s  c  r  a  t  c  h          Append letters 2 to L, one by one to the output
                                                              string where L is the length of the input string.
                                                           Append the first letter to the output string.
                           Output  c  r  a  t  c  h  s  a  y    Append “ay” to the output string.

                          Figure 8-5: How to translate an English word into pig latin

                             We’ll first append all the letters (except the first), one by one, from the
                          input word to the output word u. We then add the first letter in the input
                          word to the output v, followed by ay w. Our PigLatin procedure that imple-
                          ments these steps is shown in Figure 8-6.
                             The procedure uses three variables to create our coded words. The
                          variable outWord holds the output string as it’s assembled. A counter called
                          pos (for position) tells the script which character from the original string to
                          append to outWord. Finally, a variable named ch holds one character from
                          the input string. The procedure takes the word you want to translate into
                          pig latin as a parameter, named word.




                                                                               String Processing   189

                                           www.it-ebooks.info
   206   207   208   209   210   211   212   213   214   215   216