|
| | Program 10 - March 14
Strings and Pointers: Pig Latin
"Pig Latin" is an invented pseudo-language that is
based on manipulating the letters of an English word to produce an amusing
translation. Your assignment is to produce a program that reads lines of text
from the terminal and shows the Pig Latin translation for each line. Terminate
the program when the first character of the line is a period. The following is a
sample of what the output from your program might look like.
Enter lines of English to be translated
to Pig Latin.
Enter a period at the start of a line to exit.
Now is the time for all good men
Ownay isway ethay imetay orfay allway oodgay enmay
to come to the aid of the party.
otay omecay otao ethay aidwai ofway ethay artypay.
The quick brown fox
Ethay ickquay ownbray oxfay
jumped over the lazy dog's back.
jmpedjay overway ethay azylay ogsday ackbay.
.
Translation Rules
There are various versions of Pig Latin. Use the rules below so
that the assignment is of the same difficulty for everyone.
At a minimum, use the following rules:
-
Your program must not crash or behave in a weird manner no
matter what the user types.
-
Recognize and translate each word separately.
-
Leave all white space and punctuation in place.
-
Don't add any extra spaces.
-
If a word starts with a vowel (a, e, i, o or u) add
"way" to the end of the word. For example, over
becomes overway.
-
If a word starts with a consonant, move that consonant to
the end of the word and add "ay". For example back
becomes ackbay. If a "word" has nothing but
consonants, make sure you don't crash: you can either just leave it alone or
add "ay" as you choose. For example, NBC can be left
as is or made into NBCAY.
If you feel ambitious, add these rules as well, but only after
you have the basics working:
-
If a word starts with multiple consonants in a row, move all
the consonants to the end. For example, brown becomes ownbray
rather than rownbay. If you do this, it's better to treat
"y" as a vowel when it comes after the start of a word so that the
word is pronouncable. For example, type would become ypetay,
not petyay. You can also look for special initial sequences
like "qu" and treat them as a unit as I've done in the example
above.
-
Although it's tricky, the output looks better if you leave
the first letter as a capital if it started out that way. So you would make Charlie
into Arliechay, not arlieChay. If you do this,
you might want to make an exception for words that are all caps. So IBM
could either be left as-is or translated to IBMWAY.
-
Words with an apostrophe in them are tricky. If you treat
the apostrophe as ending the word, then you will translate don't
as onday'tay or onday't, depending on how you treat
all-consonant "words". On the other hand, if you wish, you could
recognize don't as a single word, andl translate it as on'tday.
Program Organization
Because this program is intended to deal with cstrings and
pointers, you should read each line into an array of characters. You can either
make your translation into another array or send characters directly out. Here
is a suggested program outline, although you can do this assignment successfully
in other ways:
-
Allocate a large array for character input
-
Read each line into the array, and set a pointer to the
start of the array. If it starts with a period, exit.
-
Copy any non-word characters (non-letters) till you find the
start of a word or the end of the string.
-
If it's the end of the string, go back to 2 and repeat.
-
Save a pointer to the start of the word.
-
Find the end of the word by moving your character pointer
ahead till you find a non-word character.
-
Calculate the length of the word.
-
Call your translate function, giving it the saved word
pointer and the length you calculated.
-
Your translation function should output the translation of
the word. It can make another copy of the word to work with if necessary or
go directly from the passed argument.
-
Go back to 3 and repeat.
Suggested "Project Plan"
As we have discussed in class, it's important to approach a
project like this in manageable pieces. Here is one suggested order of
programming and testing this assignment.
-
Read lines into an array and output the line one character
at a time using a pointer. This tests steps 1, 2, 3 and 4
-
Add detecting the start of a word and then finding it's end.
Use a translate function that doesn't actually do anything but just outputs
the word it is given. This tests the remaining steps.
-
Change your translation function to add "ay" to
the end of every word. Make sure that you are properly handling all
punctuation outside the word. Commmas, periods and quotation marks should
stay where they belong.
-
Change your function to distinguish between vowels and
consonants at the start of a word. Retest.
-
Decide how to handle "words" of all consonants and
modify yhour program if necessary. Retest.
-
If you want to do any of the "extras", modify your
program and retest it for each one.
When you are testing your program, be sure to test each type of
word (vowels, consonants, caps, etc.) in all possible positions: start of line,
middle of line, end of line, with and without leading space, with and without
punctuation right ahead of it or right after it.
|