The purpose of this lab is to build a program with loops. You will continue to use the other skills you have picked up over the last few weeks.
You will write a program that converts text to Pig Latin.
If you are not familiar with Pig Latin take a look at the article on Wikipedia.
As you can see from the Wikipedia article, to convert a word to Pig Latin you remove the first syllable, unless the word starts with a vowel (“a”, “e”, “i”, “o” or “u”; for our purposes here, “y” is not a vowel). Then put this first syllable at the end of the word and add the suffix “ay”. For example the first syllable of the word “stop” is “st” the rest of the word is “op”, so put the first syllable at the end and you get “op-st” then add “ay” to get “op-st-ay” and you have the pig latin translation of “stop” is “opstay”.
For our purposes we will define the first syllable as all leading consonants. If there are no initial consonants, then there is nothing to move to the end, but you still add “ay”, and in this way “is” which becomes “isay”.
To signify the end of the input, have the user input “
Furthermore, your program should continue asking for text to translate until the user asks to stop.
The output should look something like:
Please give me some text to convert into Pig Latin (end the sentence with a space and then a # or use the word “opstay” to stop): i wrote this program because i can not speak pig latin #
i wrote this program because i can not speak pig latin isay: iay otewray isthay ogrampray ecuasebay iay ancay otnay eakspay igpay atinlay inay igpay atinlay!
Please give me some text to convert into Pig Latin (end the sentence with a space and then a # or use the word "opstay" to stop):while this is fun, i think i will go do something more fun for labor day! #while this is fun, i think i will go do something more fun for labor day! isay: ilewhay isthay isay unfay, iay inkthay iay illway ogay oday omethingsay oremay unfay orfay aborlay ayday! inay igpay atinlay!
Please give me some text to convert into Pig Latin (end the sentence with a space and then a # or use the word "opstay" to stop):opstayOk, see you later!</pre>
Please note that I correctly handled the “,” and “!” at the end of the words “fun” and “day”. I did NOT produce “un,fay”. You only need to handle a single punctuation mark at the end of the word; don't worry about punctuation at the beginning or multiple marks.
Your program should handle words without any vowels such as “try,” “crypt,” and “myth” by adding “ay” to the end.
As you did in the last lab, write some test cases, that is, sentences and corresponding translations. These should start very small and simple but progressively become more complex. You will use these to test that your program does what it is supposed to.
For extra credit you can handle input with multiple punctuation at the beginning of a word, like : 'run' he said. # which should translate to : 'unray' ehay aidsay. (the leading quotation mark is preserved) 'Hey', → 'Eyhay', and not Hey','ay
Additional extra credit: Preserve capitalization at the beginning of words, like : This → Isthay
To help you get started, write a program that prints out a message “Hello, my name is Hal!” Then, on a new line, the program should print the message “What is your name?” After typing in the name, the program should print the message “Hello, user name. I am glad to meet you!
#include <iostream> #include <string> using namespace std; int main() { string name; cout << "Hello, my name is Hal!" << endl; cout << "What is your name? "; cin >> name; cout << "Hello " << name << " I am glad to meet you!"<< endl; return 0; }
For the rest of the class you will be constantly having to think about how to break the problem into smaller pieces and to solve the pieces one at a time.
In this case, try first building a program that will read in a sentence of words and print them back out until it finds the sentinel value (see sentinel values in the book).
#include <iostream> #include <string> using namespace std; int main() { string word; bool done = false; while(!done){ cout << "Please enter a sentence to pig-latinize: " << endl; while (cin >> word) { cout << "Word was: " << word << endl; if(word == "#"){ break; } } } return 0; }
Again, take this step by step. You should now be able to read in the word and print it back out.
Last you will have to find any trailing punctuation, separate it out, find the leading consonants (another loop!). Then reassemble the word, assemble a line of words and print it all out. I will let you think about how to break these parts up. Do one little piece at a time, printing out what you have so far.
If you get stuck you might try thinking about the problem in the order opposite from what I suggested above. That is, try to make a program that reads in a single word and translates it. Then a program that takes in a single line and translates each word, and finally a program that reads multiple lines as show above. This approach is called “bottom-up” because we start with the smallest, most basic step in the program and then build up from there.
The approach given in the previous section is called “top-down”, it starts with the “broad strokes” first, ignoring the fact that we do not yet know how to manipulate the most basic elements (the words in this case) yet. This approach seeks first to just to isolate them (the words).
Some programmers will do a little of both, sometimes thinking first top-down, but then writing code bottom-up. I find it useful to start coding top-down, but I write little separate test programs to make sure I can see how to do the bottom (basic) pieces too. Last I combine the pieces I have. We are all different, you will need to find your own way to think about problems and to actually write programs, but I encourage you to try to get parts working before you expect the whole program to work.
Try to discipline yourself to get some parts working before you see a TA. You might find that once something is working, that you can see how to get the next part working!
When you have your program working, you will need to show it to a TA. The TA will evaluate your code based on the following criteria:
2 points extra credit: Preserve capitalization at the beginning of words (“Kevin” → “Evinkay”, not “evinKay”)
Additional 2 points extra credit: Handle multiple punctuation marks at the beginning and ending of words correctly ( 'Hey', → 'Eyhay', and not Hey','ay)