All programming assignments must be done INDIVIDUALLY by all members of the class. Start early to ensure you have enough time to submit your program before the deadline.

All assignments (.c file) must be turned in using gradescope.com , which is a LMS (learning management system). Grade Scope is an online tool that will make it easier for us to collect homework assignments, grade them, and return them to you. You must upload your source file to Grade Scope before 11:59 pm on the due date shown above. These due dates and the time will be rigidly applied. An assignment that is not uploaded by 11:59pm on the due date will not be graded, receiving a score of 0. If you have any questions/problems with the turn-in, see one of the tutors or the instructor. 

All your programs must be neat and legible. Indent and comment your programs as shown in the text and as described in class. Make sure to thoroughly understand the assignment before you start on it. It is a good idea to verify your understanding of the problem with the tutors or instructor. For maximum credit, read and follow the style rules given in the CSE 5A Program Grading document ( http://www.gregmiranda.com/cse5a-program-grading). 

Getting Started (and make sure to START EARLY!!!)

In the lab, log into the Windows computer using your general UCSD account. 

It’s highly recommended that you create a new folder for each homework assignment in your documents folder. 

Create a new folder/directory in your documents folder named HW8 (note the uppercase HW8). You can do this by opening your Documents folder and then right click and select New -> Folder and name it HW8 . 

Launch Bloodshed Dev-C++ from the icon on the Windows desktop. 

Create a new Source File (File-New->Source File) with File Type: C Source Files (*.c), File Name: hw8.c (note lowercase hw8.c). Then Save As in the new HW8 folder you created above (File->Save As). 

Feel free to ask the tutors on duty in the lab for help. 

We also recommend that you store all your files in the cloud, either Dropbox or Google Drive so that you don’t lose your files if something happens to your UCSD account. Also, if there is an issue turning in files, we need to be able to see the timestamp of the file. We do not accept any late homework assignments, nor do we allow them to be emailed to us. 

Background – String Parsing

In programming, we often deal with strings of text, whether entered by the user or read in from a file. Even Amazon Alexa and Google Home convert speech into strings of text which are then parsed and used to answer questsions and perform commands. 

When parsing a string we start at the beginning and read each word until we reach the end of the string, which is either a newline character or the NULL character (‘\0’ or 0). Each word is called a token. The token is then used for some purpose. In our case we will convert the word into Pig Latin or into English. 

Tokens are separated by delimeters. A delimeter is a character that is a character that is not used in the string itself. Typically whitespace (tabs, spaces, and newlines) are used as delimters. In some cases, like Excel CSV (comma separated values), commas and newlines are used as delimters. When you reach a delimter, you’ve reached the end of the token. In our case, we will be using spaces and newlines as our delimters. When ever we reach a space or a newline, we’ve reached the end of the word. 

As is the case with all problems in programming, there are many different ways to parse a string into tokens. It’s such a common activity that there is a function called strtok() in the Std C Lbrary which will parse a string into tokens, one at a time. Another way to parse a string into tokens is to call sscanf() which works just like scanf() but instead reads from a string insead of the console. More about those functions can be found online, however, we will not be using either method for this assignment. 

Instead we will be creating our own tokenizer (function that reads tokens) called getToken() . This function will take in a string, search for the delimter, terminates the token by adding a NULL character, and then returning the token that was found. More details can be found below in the write-up regarding getToken() . 

Program Description

Homework 8 (hw8.c) will be an English-to-Pig Latin and Pig Latin-to-English translator. The program will use functions, strings (arrays of characters), pointers, and string manipulation library functions. 

You can download an example executable for Homework 8 from the following link: http://www.gregmiranda.com/cse5a-hw8-sample 

Your output should match the sample program exactly. Make sure to compare the output of the sample and your program side by side to confirm that your program matches. 

Starter Code

Below is some starter code to get you starter. Make sure to fill in the header at the top of the file (see below).
/*

* File: hw8.c

* SID:Your student ID number (replace this!)

* Name:Your name (replace this!)

* Date:Month Day, Year (Date when file was created) (replace this!)

* Sources of help:All sources of help you used (tutors, Piazza posts, books,

*                           websites, etc.). Other students should not be listed here as

*                          other students should not be looking at your code.

*                          If you did not use any sources of help, you MUST put "None".
*
*
General description of the program ... (replace this!)

*/


#include <stdio.h>

//Constants 

//TODO: any constants should go here using #define (remove this line)

//Function Prototypes

//TODO: any function prototypes should go here – no function bodies (remove this line)
/*

* General description of the main function ...

*/
int main()

{

//TODO: the main program driver should go here (remove this line)

return 0;
}

//Function Definitions

//TODO: any function definitions should go here – i.e. the function bodies as well as function headers (remove this line)

Coding Tips

For counting variables, use integers

To change a value in a function, pass it using a pointer (remember that arrays are pointers)

Declare variables at the beginning of functions (like main())

Program Description

This program will be an English-to-Pig Latin and Pig Latin-to-English translator. The user will have the option to select which translation to perform and input lines of English or Pig Latin phrases that will be translated to Pig Latin or English one line at a time until the user indicates there is no more input. 

For our purposes we will use the following algorithm to translate an English word to a Pig Latin word: Take the first character of the English word and move it to the end of the English word and add "ay". If the original English word started with a capital/uppercase character, then the resulting Pig Latin word will also start with a capital/uppercase character and the first character that is moved to the end is converted to lowercase. To translate a Pig Latin word to English, reverse the process. 

1) Declare any pre-processor directives and constants that you will need at the top of the program (under the file header). No magic numbers allowed other than 1, -1, or 0. Everything else must be a constant.

You will need to include the following Std Library header files for the various library functions you will use:

#include <stdio.h>

#include <string.h>

#include <ctype.h>

Here are some helpful constants:

#define MAX_STRING 81

#define MAX_WORDS 50

2) Add the following function prototypes to your code before main():

//Function Prototypes

void displayMenu();


void translatePhrase(_Bool translateEnglish);

char* getToken(char* str, _Bool* pEnd);


void englishToPigLatin(char englishWord[], char pigWord[]);

void pigLatinToEnglish(char pigWord[], char englishWord[]);

3) Create your main() function with a return type of void (or int ) which takes no parameters. If you use a return type of int, make sure to include a return 0 ; at the end of your main() function. Inside the main() declare any variable that you will need. Display the welcome message.

Welcome to the Pig Latin translator.

4) Next, display the menu. To display the menu, call the displayMenu() function. The menu should run in a loop until the user inputs ‘q’ or ‘Q’ to quit. The menu will print a prompt for the user to tell them what they should do.

5) After displaying the menu, get the user’s input. You can do this any way you want, including using the getCharInput() function from previous homework assignments. Make sure to save the user’s input into a variable. Also make sure to eat the extra newline character. 

6) Use an if/else if/else or a switch() statement to process the user’s choice.

a. If the user enters ‘A’ (or ‘a’):

i. Call the translatePhrase() function. Make sure to set the parameter to true (1).

b. If the user enters ‘B’ (or ‘b’):

i. Call the translatePhrase() function. Make sure to set the parameter to false (0).

c. If the user enters ‘Q’ (or ‘q’):

i. Say Exiting… and make sure the loop ends.

d. Anything else, print out an error:

Invalid command

7) Create a function called displayMenu() with a return type of void which takes no parameters. Your function declaration should look like:

void displayMenu()

In this function, you will display the menu of options to the user. The menu should appear as:

A) Translate from English into Pig Latin

B) Translate from Pig Latin into English

Q) Quit


Enter command:

8) Create a function called translatePhrase() with a return type of void which takes one parameter: a _Bool. Your function declaration should look like:

void translatePhrase(_Bool translateEnglish);

This function will prompt the user to enter English or Pig Latin Phrases, based on the translateEnglish parameter (a value of 1 for English and a value of 0 for Pig Latin). 

In a loop, read up a line of input using fgets() until there is no more input as indicated by the user entering <Control>Z at the keyboard in the lab (Windows) or <Control>D if running on Unix or a Mac. 

In a nested loop, read each word in the line getToken() until the end of the line is reached, i.e. when pEnd is true (has a value of 1). Note, after reading each word, you will need to call getToken() with a pointer to the start of the next word. Use strlen() to get the length of the word, add one to skip the NULL character, and add that value to the pointer used to read the last word. Remember, if you add an integer to a pointer (and an array is a pointer) you get a pointer to another part of the same array. 

Also, do not use an array for the return value of getToken() , just define a char* variable that is uninitialized. That vairable will then point to the word inside the array that was filled in by the call to fgets() earlier.

Call either englishToPigLatin() or pigLatinToEnglish() with each word depending on the type of translation (from the translateEnglish  parameter). 

Output the translated word with a trailing space and loop back to parse and translate the next word. 

When all the words on the line have been translated and output, loop back to read the next line. 

9) Create a function called getToken() with a return type of char* which takes two parameters: a char* and a _Bool*.
Your function delcaration should look like:
 

char* getToken(char* str, _Bool* pEnd); 

This function will find the end of the next token (word) and return it. It will also look for the end of the line by checking for the newline character (‘\n’). 

The start of the token is the initial value of str. Save it immediately and return it at the end of the function. 

Each token is separated by a space or a newline character. Using the str parameter, advance the pointer (in a loop) until one of the separators is reached. 

Modify pEnd based on the value of the separator. If it’s a newline character change pEnd to value of 1 (true), or 0 (false) if it’s a space. 

Finally, change the separator to the NULL character (‘\0’ or 0). This will terminate the word making it an independent string. 

10) Create a function called englishToPigLatin() with a return type of void which takes two parameters: both char arrays.
Your function declaration should look like:

void englishToPigLatin(char englishWord[], char pigWord[]);

This function will translate the English word in the first array into the Pig Latin word, storing it in the second array. There are several ways to order the operations that need to be performed. You are given the latitude to decide what works best for you. Basically, you need the first character of the English word passed in the englishWord[] array moved to the end of the word with the “ay” appended as the Pig Latin translation in the pigWOrd[] array. 

Preserve capitalization of the first character of the word in the translation. For example, Computer becomes Omputercay. 

You will most likely want to use the Std C Library routines, strcpy()/strncpy(), strlen(), isupper(), toupper(), tolower(), and possibly strcat()/strncat().
11) Create a function called pigLatinToEnglish() with a return type of void which takes two parameters: both char arrays

Your function declaration should look like:

void pigLatinToEnglish(char pigWord[], char englishWord[]);

12) This function will reverse the translation taking a Pig Latin word and translating it back into English. You may assume all Pig Latin words passed to this function and entered in this program are well-formed and correct Pig Latin words. 

The third from the last character in the Pig Latin word is the first character in the English word. The characters up to this third from the last character forms the rest of the English word. Preserve capitalization. For example, Omputercay becomes Computer. 

NOTE:Please follow the instructions on how to write this program. This will make grading much easier.
Comment your program like the last program (and the Program Grading guide). Make sure to use meaningful comments.
------------------------------------------------ Test Execution ------------------------------------------------
Welcome to the Pig Latin translator.


A) Translate from English into Pig Latin
B) Translate from Pig Latin into English
Q) Quit


Enter command:
c


Invalid command

A) Translate from English into Pig Latin
B) Translate from Pig Latin into English
Q) Quit


Enter command:
a 


Enter English phrases:
Hello World
Ellohay Orldway


nix tell
ixnay elltay


be rar KO
ebay array Okay


I like this Computer Programming gig
Iay ikelay histay Omputercay Rogrammingpay iggay


^Z


A) Translate from English into Pig Latin
B) Translate from Pig Latin into English
Q) Quit


Enter command:
b


Enter Pig Latin phrases:
Iay ikelay histay Omputercay Rogrammingpay iggay
I like this Computer Programming gig


ixnay elltay
nix tell


^Z
A) Translate from English into Pig Latin
B) Translate from Pig Latin into English
Q) Quit


Enter command:
q


Exiting...

Extra Credit – 5%

Getting Started:
To start, copy your hw8.c file to a new file called ec_hw8.c. 

Important:Your original hw8.c file must remain unchanged. You need both files for turnin. 

EC Requirements:

Add three more function prototypes to your program:

_Bool isPigLatinWord(char word[]);

void isPigLatin();

void autoTranslatePhrase();

Add the following to the displayMenu()function the appropriate location (before Quit):

C) Is Phrase Pig Latin?

D) Translate Phrase Automatically

• To main(), add two more cases for the above menu items:

• If the user enters ‘C’ (or ‘C’):

 Call the isPigLatin()  function.

• If the user enters ‘D’ (or ‘d’):

Call the autoTranslatePhrase() function.

• Create a function called  isPigLatin() with a return type of  void which takes no parametersYour function declaration should look like:

void isPigLatin(); 

This function will prompt the user to enter phrases. 

In a loop, read up a line of input using fgets() until there is no more input as indicated by the user entering <Control>Z at the keyboard in the lab or <Control>D if running on Unix or a Mac. 

In a nested loop, read each word in the line using sscanf(). 

Call isPigLatinWord() to determine if the word is Pig Latin (return value of 1) or English (return value of 0). 

When all the words on the line have been translated, output “Pig Latin” if every word in the phrase was in Pig Latin and output “Not Pig Latin” if any word in the phrase was English. 

Loop back to read the next line. 

Create a function called isPigLatinWord() with a return type of _Bool which takes one parameter: a char array.
Your function declaration should look like: 

_Bool isPigLatinWord(char word[]); 

This function will determine if the word is Pig Latin or English. If the word is Pig Latin (ends in “ay”), then return a value of 1. Otherwise, return a value of 0. 

Note: there are English words that end in “ay”, such as “way”. For the purpose of this assignment, assume that any word that ends in “ay” is Pig Latin.
Create a function called isPigLatin() with a return type of void which takes no parameters. Your function declaration should look like: 

void autoTranslatePhrase(); 

This function will prompt the user to enter phrases. 

In a loop, read up a line of input using fgets() until there is no more input as indicated by the user entering <Control>Z at the keyboard in the lab or <Control>D if running on Unix or a Mac.

In a nested loop, read each word in the line using sscanf() . Make sure to store the word in an array of strings (a multi-dimensional array). Also, keep a count of how many words were stored in the array. 

Call isPigLatinWord() to determine if the word is Pig Latin (return value of 1) or English (return value of 0). 

When all the words have been scanned in and saved, use a for loop to translate and output all the words which were scanned in earlier. Note: do not use sscanf() again to read in each word. Call either englishToPigLatin() or  pigLatinToEnglish() with each word depending on the type of translation. 

Output the translated word with a trailing space and loop back to parse and translate the next word.

When all the words on the line have been translated and output, loop back to read the next line. 

------------------------------------------------ Test Execution ------------------------------------------------
Welcome to the Pig Latin translator.

A) Translate from English into Pig Latin
B) Translate from Pig Latin into English
C) Is Phrase Pig Latin?
D) Translate Phrase Automatically
Q) Quit
Enter command:
c

Enter phrases:
Hello World
Not Pig Latin

Ellohay Orldway
Pig Latin

I like this Computer Programming gig
Not Pig Latin

Iay ikelay histay Omputercay Rogrammingpay iggay
Pig Latin

^Z
A) Translate from English into Pig Latin
B) Translate from Pig Latin into English

C) Is Phrase Pig Latin?
D) Translate Phrase Automatically
Q) Quit
Enter command:
d

Enter phrases:
Hello World
Ellohay Orldway

Ellohay Orldway
Hello World

I like this Computer Programming gig
Iay ikelay histay Omputercay Rogrammingpay iggay

Iay ikelay histay Omputercay Rogrammingpay iggay
I like this Computer Programming gig
^Z
...
You can download an example executable for Homework 8 Extra Credit from the following link: http://www.gregmiranda.com/cse5a-ec-hw8-sample

Homework Turnin

Before turning in your assignment, make sure that your program meets the style and documentation guidelines given in the CSE 5A Program Grading document (http://www.gregmiranda.com/cse5a-program-grading). Also, make sure you compile and run your program before turning it to make sure that it still works properly. Programs that do not compile will receive a 0 for correct output.  

Once you have completed your homework assignment, you need to turn it in to gradescope.com. You should have received an email from gradescope.com which will allow you to set your password and log in. If you have not received the email from Gradescope, contact the instructor immediately. 

From the dashboard, select your course, CSE 5A, by clicking on it. Next, click on the correct assignment. 

To submit your assignment, either drag your files into the Drag & Drop box, or click on the box, and then navigate to your files and select them. Your file names must match the file names below *exactly*. 

Once you have selected all the files you want to upload, click on the Upload button. Once all the files have been uploaded, you will see a new dialog box saying that your assignment has been submitted successfully. You can review your files by clicking on the Code button in the upper right. 

You can resubmit your homework assignment as often as you want until the due date time has expired. To resubmit, click on the Resubmit button in the bottom right corner and reselect all the files you want to resubmit. You must resubmit all files each time. 

Your file names must match the file names below *exactly*. 

Files required for the Turn-in:
hw8.c
Extra Credit Files:
ec_hw8.c

Due Date: Sunday night, November 24 @ 11:59 pm

Make sure to make a backup of all your files (before the due date) on a cloud storage service such as Dropbox or Google Drive.

If there is anything in these procedures which needs clarifying, please feel free to ask any tutor, the instructor, or post on the Piazza Discussion Board. 


NO EXCUSES!
NO EXTENSIONS!
NO EXCEPTIONS!
NO LATE ASSIGNMENTS ACCEPTED!
DO NOT EMAIL US YOUR ASSIGNMENT!
Start Early, Finish Early, and Have Fun!




2019-11-22