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