Lab Assignment 6

  1. [solve] Each September 19 is International Talk Like a Pirate Day. The goal of this lab is to translate text into pirate speak by making a few substitutions. The assignment describes a collections of functions to define. Please follow the design, and define those functions. Put all of the functions into one program.

    1. Recognizing a letter

    You can compare characters to see if one is less than another. The order is alphabetic. For example, 'b' < 'e'. But there is a catch. All of the upper case letters are defined to be smaller than all of the lower case letters. So 'Z' < 'a', since upper case letter 'Z' is less than lower case letter 'a'.

    Write a function isLetter(c) that yields true if character c is a letter and false if c is not a letter.

    (Hint. c is a letter if either 'a' ≤ c ≤ 'z' or 'A' ≤ c ≤ 'Z'. Remember how to convert that to Cinnameg.)

    Test this function by including the following tester.

    Execute
      Displayln "isLetter('a') = " ++ $(isLetter('a')).
      Displayln "isLetter('r') = " ++ $(isLetter('r')).
      Displayln "isLetter('A') = " ++ $(isLetter('A')).
      Displayln "isLetter('Z') = " ++ $(isLetter('Z')).
      Displayln "isLetter('.') = " ++ $(isLetter('.')).
      Displayln "isLetter(' ') = " ++ $(isLetter(' ')).
    %Execute
    

    2. Getting a word

    For the purposes of this assignment, a word is a sequence of letters that is surrounded by nonletters (or by the beginning or end of the string).

    Write a function wordCount(s) that produces the number of characters in the word at the beginning of string s. For example, wordCount("The rabbit ran into the brush") = 3, since the first word has 3 letters in it, and wordCount("Make me an offer") = 4. If wordCount is given a string that does not start with a letter, it should yield 0. For example, wordCount(". My word!") = 0. Your wordCount function should also yield 0 when the string is empty.

    (Hint. Start at 1 and look through the characters of the string until you see a nonletter or until you reach the end of the string. This is a search problem. Be sure to remember that you might hit the end of the string before you find a nonletter.)

    Remove the test of isLetter and replace it by the following to test wordCount. (You include a quote mark inside a string by writing \".)

    Execute
      Displayln "wordCount(\"The green frog\") = " ++ $(wordCount("The green frog")).
      Displayln "wordCount(\"green frog\") = " ++ $(wordCount("green frog")).
      Displayln "wordCount(\"green\") = " ++ $(wordCount("green")).
      Displayln "wordCount(\"-green frog\") = " ++ $(wordCount("-green frog")).
      Displayln "wordCount(\"\") = " ++ $(wordCount("")).
    %Execute
    

    3. Getting a nonword

    Make a copy of your wordCount function. (Just copy and paste.) Modify the copy to define function nonwordCount(s), which yields the number of nonletter characters at the beginning of string s, up to the first letter or the end of the string. For example, nonwordCount("+*kangaroo") = 2, since there are two nonletter characters before the first letter. You should find the change to be very simple.

    4. Translating a word

    Write a definition of function pirateWord(w) that takes a word and produces either the same word or a different word. The rules are:

    pirateWord("is") = "be"
    pirateWord("Is") = "Be"
    pirateWord("are") = "be"
    pirateWord("Are") = "Be"
    pirateWord("there") = "thar"
    pirateWord("my") = "me"
    pirateWord("Hello") = "Ahoy"
    pirateWord("friend") = "matey"
    pirateWord("you") = "ye"
    pirateWord("You") = "Ye"

    In all other cases, pirateWord(s) = s. For example, pirateWord("kangaroo") = "kangaroo". You will find definition by cases to be convenient.

    Replace your tester with the following one and test the definition of pirateWord.

    Execute
      Displayln "pirateWord(\"is\") = " ++ $(pirateWord("is")).  
      Displayln "pirateWord(\"there\") = " ++ $(pirateWord("there")).  
      Displayln "pirateWord(\"kangaroo\") = " ++ $(pirateWord("kangaroo")).  
    %Execute
    

    5. Translating text

    Write a definition of function pirateTalk(s) that takes a string s and translates all words in s to pirate words. It should yield the modified string. For example, pirateTalk("Hello, friend. You are welcome on my ship.") = "Ahoy, matey. Ye be welcome on me ship."

    (Hint. Accumulate the answer string. Start with it set to an empty string. Then repeat the following until there is nothing left to translate.

    First count the number of characters in the first word. Get the word (a substring) and get what is left after the word (another substring). Then count the number of nonword characters that come after the word. Get the nonword, and also what follows the nonword. Now you have three strings: the word, the nonword, and the rest of the string (after the word and the nonword). Get the pirate word. Concatenate the pirate word and the nonword to the end of your result string. Continue with what is left, translating that.

    Stop when there is nothing left to translate. Your result string is the answer.

    6. The final program

    To finish, define some text and make the program display the translation of that text. You can choose the text, but choose something that involves making some changes. Remember that, if there is anything wrong with your program, you want to find it out so that you can fix it. Your Execute part will be as follows.

    Execute
      Let text = "...your text here...".
      Displayln pirateTalk(text).
    %Execute
    
    When you are satisfied that the program works, submit it.