Purpose

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.

Requirements

Intro to Pig Latin

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”.

When to Stop Taking Input

To signify the end of the input, have the user input “

  1. ” at the end.

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): 
opstay
Ok, see you later!
</pre>

Punctuation

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.

Words Without Vowels

Your program should handle words without any vowels such as “try,” “crypt,” and “myth” by adding “ay” to the end.

Test Cases

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.

Extra Credit

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

Useful Tips and Methods

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;
}

Think before programming

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.

Getting Help

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!

Pass-off procedure

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:

  1. Show the TA your test cases, with the input and correct output (manually written). You will be graded on the diversity of your tests. You should have about 5-10 tests. Note that you get these points for just writing the test cases, no programming required. (5 points)
  2. As you start to write a program to do the conversion, start with small simple steps as listed above. Note that you do not have to show the TA each step individually. The TA will simply check that your program accomplishes each of these elements:
    1. Ask for text to convert until the user responds with “opstay” (5 points)
    2. Isolate each word in the text given by the user (5 points)
    3. Convert words that start with a vowel (that means you just add “ay”) (5 points)
    4. Convert words that start with one consonant (5 points)
    5. Convert words that start with two or more consonants (3 points)
    6. Convert words that are followed by a punctuation mark (anything that is not a letter) (3 points)
  3. Is your code properly formatted (indenting blocks, comments, spacing, reasonable names of variables)? (5 points)

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)

cs-142/simple-pig-latin.txt · Last modified: 2015/01/07 08:57 by ryancha
Back to top
CC Attribution-Share Alike 4.0 International
chimeric.de = chi`s home Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0