Problem Statement (Write a Program in C that does the following): Computers have
ID: 3680771 • Letter: P
Question
Problem Statement (Write a Program in C that does the following):
Computers have long been used to encode and decode secret messages. One of the simplest "encryption" schemes is Pig Latin. There are various rules for translating English to Pig Latin. For this assignment, we use the following rules:
1. If a word starts with a vowel (not including 'y') and ends in a consonant, simply append "ay" to the end of the word. Example: insect becomes insectay.
2. If a word starts and ends with a vowel (not including 'y'), simply append "hay" to the end of the word. Example: else becomes elsehay.
3. Otherwise, remove the consonants up to the first vowel of the word, append them to the end of the word, and then append "ay". Example: clog becomes ogclay.
4. All spacing and punctuation is retained.
5. Capitalization should be retained. Example: Deborah Hwang becomes Eborahday Anghway.
Assignment
Write a Pig Latin translator in C that will repeatedly read lines of text from the console and translate them until the user enters "QUIT". You may assume that each line of original or translated text will be no more than 1000 characters. It is highly recommended that the following functions be written and in the order listed. Please note that while these exact functions are not required, programs that do not make use of functions as appropriate will not earn full credit for analysis and design.
A function is_vowel that receives a character and returns true if the character is a vowel, and returns false otherwise.
A function get_next_word that receives an English sentence as a string and a start index, and passes back the next word (i.e., consecutive alphabetic characters) that starts at the start index in the sentence as a (separate) string. The start index is assumed to be the index of a character that is a letter.
A function get_next_nonword that receives an English sentence as a string and a start index, and passes back a (separate) string of all the nonalphabetic characters that occur starting at the start index. The start index is assumed to be the index of a character that is not a letter.
A function translate_word that receives an original English word as a string and passes back the corresponding Pig Latin word translation as a (separate) string.
A function translate that receives an original English sentence as a string and passes back the corresponding Pig Latin sentence translation as a (separate) string. The main idea of this function is to alternate calling get_next_word and get_next_nonword keeping track of the starting index of each word/nonword until reaching the end of the original English sentence string. A word is translated before concatenating it to the result Pig Latin sentence string, while a nonword is concatenated to the result string as is.
It is recommended that each function above be tested for correctness before going on to the next function. The final main program is responsible for repeatedly reading in a line of English text, calling the translate function, and displaying the resulting Pig Latin text.
The output of the program must conform to the following example (user input in bold).
Igpay Atinlay Anslatortray
Please enter a sentence to translate (QUIT to exit):
The quick brown fox jumped over the lazy dog.
The translation is:
Ethay uickqay ownbray oxfay umpedjay overay ethay azylay ogday.
Please enter a sentence to translate (QUIT to exit):
A man, a plan, a canal, Panama. Palindrome!
The translation is:
Ahay anmay, ahay anplay, ahay analcay, Anamapay. Alindromepay!
Please enter a sentence to translate (QUIT to exit):
The University of Evansville pig latin translator ... is online.
The translation is:
Ethay Universityay ofay Evansvillehay igpay atinlay anslatortray ... isay onlinehay.
Please enter a sentence to translate (QUIT to exit):
Wile E. Coyote, Super-Genius
The translation is:
Ileway Ehay. Oyotecay, Upersay-Eniusgay
Please enter a sentence to translate (QUIT to exit):
QUIT
Coding Notes
There is a newline (that causes a blank line) before the prompt for input and before the output.
There is a newline at the end of the prompt for input. It should be written immediately after the colon. I.e., there is no space after the prompt
The line of input must be read in using fgets, since the input may have spaces. Reminder: fgets stores the newline from when the user presses the Enter key.
Reminder: make sure the constructed strings end with the nullterminator ('').
Explanation / Answer
This is one of the programs i have got for translating text into pig latin.I have tried my best.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
typedef struct node
{
char word[20];
struct node * link;
}
node;
typedef struct queue
{
node * front;
node * rear;
} queue;
queue *input = NULL, *trans = NULL;
void break_and_translate(char * phrase, int z);
queue * buildPhrase(queue * abcd, char data[20]);
node * translate(node * data);
node * translateBack(node * nextWord);
void displayP(node * abcd);
int isWordSep(char a);
int isVowel(char a);
int main()
{
int ch, ch2;
char dummy;
char * phrase;
do
{
printf(" 1. Input phrase 2. Translate phrase 3. Display phrase 4. Display Translation 5. Clear current phrase and translation 6. Exit ");
printf(" Enter your choice: ");
scanf("%d", &ch);
scanf("%c", &dummy);
switch(ch)
{
case 1:
phrase = (char *)malloc(1024 * sizeof(char));
printf("Input the phrase to be translated. ");
gets(phrase);
if(phrase==NULL)
printf("Sorry, cant read the phrase. Try again.");
else
printf("Phrase recieved!!!! ");
break;
case 2:
printf("Are you tanslating from: 1. English into pig latin 2. Pig latin to english ");
scanf("%d", &ch2);
scanf("%c", &dummy);
break_and_translate(phrase, ch2);
if(input ==NULL || trans ==NULL)
{
printf("Translation uncessful. Rerun the program and try again! ");
exit(0);
}
else
printf("Translation sucessfull!!!! ");
break;
case 3:
if(phrase==NULL)
printf("No phrase entered. Please enter a phrase to translate ");
else
printf("%s ", phrase);
break;
case 4:
if(input==NULL)
printf("Translation has not been prepared yet. Please either enter a phrase to translate, or translate the current phrase. ");
else
displayP(trans->front);
break;
case 5:
if(input ==NULL && trans ==NULL)
{
if(phrase == NULL)
printf(" Nothing to reset yet! Enter and translate a phrase! ");
else
printf("No tanslation yet, clearing the input phrase. ");
}
input = trans = NULL;
free(phrase);
phrase = NULL;
printf("Reset sucessfull!!!! ");
break;
case 6:
printf("Goodbye!!!! ");
exit(0);
default:
printf(" Invalid choice. Please try again... ");
}
}
while(1);
return (ch);
}
queue * new_queue()
{
queue *s = (queue *)malloc(sizeof(queue));
s->front = NULL;
s->rear = NULL;
return s;
}
queue * buildPhrase(queue * abcd, char data[20])
{
if(abcd==NULL)
{
abcd=new_queue();
}
if(abcd->rear == NULL)
{
abcd ->rear = (node *)malloc(sizeof(node));
strcpy(abcd->rear->word, data);
abcd -> rear->link = NULL;
abcd ->front = abcd ->rear;
}
else
{
abcd ->rear->link = (node *)malloc(sizeof(node));
abcd ->rear = abcd ->rear->link;
strcpy(abcd->rear->word, data);
abcd ->rear->link = NULL;
}
return abcd;
}
node * translate(node * nextWord)
{
char a; int b, c=0;
a=nextWord ->word[0];
node * latin = (node *)malloc(sizeof(node));
if(isWordSep(a)==0)
{
for(b=1; b<strlen(nextWord->word); b++)
{
latin->word[b-1] = nextWord->word[b];
c=b;
}
if((a>=65) && (a<= 91))
{
latin->word[0] = (latin->word[0]-32);
latin->word[c++] = (a+32);
}
else
latin->word[c++] = a;
if(isVowel(a)==1)
{
latin->word[c++]='t';
}
latin->word[c++]='a';
latin->word[c++]='y';
}
return latin;
}
void break_and_translate(char * phrase, int z)
{
int space =0, b=0, c=0;
char end, word[20];
memset(word, '', strlen(word));
end = phrase[0];
while(end != '')
{
end = phrase[b];
printf("END: %c ", end);
if(isWordSep(end)==0)
{
word[space]= end;
space++;
printf("WORD: %s strlen WORD: %d ", word, strlen(word));
}
else
{
node * temp = (node *)malloc(sizeof(node));
node * temp2 = (node *)malloc(sizeof(node));
strcpy(temp->word, word);
if(z==1)
temp2 = translate(temp);
else
temp2 = translateBack(temp);
c = strlen(temp2->word);
temp2->word[c]= word[space] = end;
strcpy(temp->word, word);
// printf(" %s temp word %s temp2 word ", temp->word, temp2->word);
input = buildPhrase(input, temp->word);
trans = buildPhrase(trans, temp2->word);
memset(word, '', strlen(word));
free(temp);
free(temp2);
space=0;
printf("WORD: %s ", word);
}
b++;
}
}
void displayP(node * abcd)
{
int a = 0;
node *ptr = (node *) malloc(sizeof(node));
ptr = abcd;
printf(" ");
while(ptr != NULL)
{
printf("%s",ptr->word);
a++;
ptr = ptr->link;
}
free(ptr);
}
node * translateBack(node * nextWord)
{
char a, x, y, z; int b, c=0, d;
node * latinBack = (node *)malloc(sizeof(node));
strcpy(latinBack->word, nextWord->word);
z = latinBack->word[0];
d = strlen(latinBack ->word);
if(isWordSep(z)==0)
{
a=latinBack ->word[d-3];
if((isVowel(a)==0)&&(a!='t'))
{
latinBack->word[d-2] = latinBack->word[d];
}
else if(a=='t')
{
x = nextWord ->word[d-4];
y = nextWord ->word[d-5];
if((isVowel(x)==1)&&(isVowel(y)==1))
{
latinBack->word[d-3] = latinBack->word[d];
latinBack->word[d-2] = '';
a=x;
}
else
latinBack->word[d-2] = latinBack->word[d];
}
latinBack->word[d-1] = '';
latinBack->word[d] = '';
for(b=strlen(latinBack->word); b>0; b--)
latinBack->word[b] = latinBack->word[b-1];
latinBack->word[0] = a;
latinBack->word[strlen(latinBack->word)-1]='';
z=latinBack->word[1];
if((z>=65) && (z<= 91))
{
latinBack->word[0]= (latinBack->word[0]-32);
latinBack->word[1]= (latinBack->word[1]+32);
}
}
return latinBack;
}
int isWordSep(char a)
{
if ((a==' ')||(a=='.')||(a==',')||(a=='!')||(a==';')||(a=='?')||(a==' ')||(a==''))
return 1;
else
return 0;
}
int isVowel(char a)
{
if((a == 'a')||(a=='e')||(a=='i')||(a=='o')||(a=='u')||(a == 'A')||(a=='E')||(a=='I')||(a=='O')||(a=='U'))
return 1;
else
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.