Page 212 - Learn To Program With Scratch
P. 212

Initialize outWord to an empty
                                                            string and set pos to 2 to access
                                                             the second letter of word.


                                                             Append letters [2,3,4, ... , end],
                                                            one by one, from the input string
                                                             to outWord.




                                                            Append word’s first letter to outWord.


                                                            Append ay to outWord.

                         Figure 8-6: The PigLatin procedure

                             First, the procedure creates an empty string for outWord and sets pos
                         to 2 u. (An empty string is string that does not contain any characters; its
                         length is 0.) The procedure then uses a repeat block to append all  letters
                         but the first from the input string (word) to the output string (outWord) v.
                         We skipped the first character, so the repeat count is one less than the
                         length of the input string. For each loop iteration, one character of word
                         is appended to outWord. At the end of the loop, the first letter of word is
                         appended to outWord w, along with the letters ay x.



                                                   try it out 8-2

              PigLatin .sb2  Load PigLatin.sb2 and run it to test this procedure . The application asks for an input
                            word and then says its pig latin translation . Modify it to translate a phrase, like
                            “Would you like some juice?” into pig latin . (Hint: Call PigLatin for each word to
                            assemble the output phrase .) As another challenge, write a procedure that takes a
                            pig latin word as input and shows its original English word .



                         Fix My Spelling
            FixMySpelling   In this section, we’ll develop a simple game that generates misspelled
                     .sb2  words and asks the player to enter the correct spelling. The game will cre-
                         ate misspelled words by inserting a random letter at a random position in
                         an English word. Of course, there could be more than one correct spelling
                         of misspelled simple words. For example, if the original word is wall and
                         the game produces mwall, either mall or wall would be correct. To keep our
                         game simple, we’ll ignore that possibility and insist on a particular spelling
                         for the correct answer.


           190   Chapter 8
                                           www.it-ebooks.info
   207   208   209   210   211   212   213   214   215   216   217