Lab 8 - Pig Latin

"Oneyay allsmay epstay orfay anmay…"

Due: 11 PM on Friday 11/19

Part A: Generalized Pig Latin Converter

Write and test a C function that translates a single English word to Pig Latin. The traditional rules of Pig Latin are

  1. If the word begins with a consonant - such as "hello", "this" - divide the word at the first vowel, swapping the front and back parts of the word and append "ay" to the word. The previous 2 examples would produce "ellohay" and "isthay".
  2. If the word begins with a vowel - such as "am", "are", "i" - append "yay" to the word. The previous examples would produce "amyay", "areyay" and "iyay".
  3. If the word has no vowels - such as "my", "thy" - append "yay" to it. The examples would produce "myyay" and "thyyay".
To aid in the implementation of the "translateWord" function, I suggest creating a function "findFirstVowel" to find the first vowel in a word.

Part B: Application to Translate Sentences

Write a C application that translates English sentences into Pig Latin. The user should be able to repeatedly enters sentences for translation with translations being immediately displayed. After each sentence is translated, the user is asked if they would like to input another sentence for translation. You may assume that the user does NOT type any punctuation marks in each sentence.

To reduce the complexity of the "main" function , you can split the work among several functions. This is always a good idea, especially when nested loops are involved. The inner-loop probably does a specific task which can be split off and performed by a calling a function to do that task. A good rule of thumb would be to limit each function to a single loop. This process in know as "procedural decomposition."

In our application the "main" function will have a loop that repeatedly allows the user to: enter a sentence, converts the sentence to Pig Latin, and displays the initial and coverted sentences. Since our translateWord function is only able to translate a word at a time, you must repeated call the translateWord function for each word in the sentence. To avoid the confusion of having a nested loop in the main function, call a function named "translateSentence" to translate a whole sentence.

The implementation of the "translateSentence" function can be done "easily" by utilizing the "strtok" function from the string.h string library. The following example is discussed on page 345 of the text.

/* Fig. 8.29 on page 345 of text: Example using strtok */

#include <stdio.h>

#include <string.h>

int main() {

char string[] = "This is a sentence with 7 tokens"; /* initialize array string */

char *tokenPtr; /* create char pointer */

printf( "%s\n%s\n\n%s\n", "The string to be tokenized is:", string,

"The tokens are:" );

tokenPtr = strtok( string, " " ); /* begin tokenizing sentence */

/* continue tokenizing sentence until tokenPtr becomes NULL */

while ( tokenPtr != NULL ) {

printf( "%s\n", tokenPtr );

tokenPtr = strtok( NULL, " " ); /* get next token */

} /* end while */

return 0; /* indicates successful termination */

} /* end main */

The strtok function allows you to extract a word at a time from the sentence. Each word can then be translated using the translateWord function with the translated word appended to the resulting translated sentence. An algorithm for the "translateSentence" function would look something like

Initialize an empty result string

For each word in the input string

Translate the word using the translateWord function

Append the translated word to the result string

end for

return the result string

Again, use the Linux system to edit, compile, execute, and debug your programs.

To submit your assignment, click on the link at the bottom of the homework page for the course: http://www.cs.uni.edu/~fienup/cs036f04/homework/index.htm

You will need to

  1. log on to the submission system using your CNS account information
  2. select the "810:036, C++ Fienup" course
  3. select the radio button corresponding to lab being submitted
  4. enter the name of the file you wish to submit which needs to be accessable on the computer from which you are submitting