For this exercise, write a program in c that translates from theEnglish language
ID: 3615660 • Letter: F
Question
For this exercise, write a program in c that translates from theEnglish language to another popular language "Pig Latin".the program will use the rules for following
1) if the English word begins with a consonant, then the firstletter is placed at the end of the word and the suffix "ay" isappended.
2) if the English word begin with a vowel, then the first letter isplaced at the end of the word and the suffix "tay" is appended.
3) if the English word is one character long, then the suffix "tay"is appended.
The program must be able to use correct punctuation andcaptitalization as shown in the above example. For example, theword "Place" should become "Lacepay" and not "lacePay" in order tokeep the capitalization and punctuation consistent. The onlypunctuation you need to worry about are period, commas, colons, andsemi-colons.
Implementation:
create two standard queues, implemented using singly linked lists,to hold your phrases.
The head pointer to the first queue should be named EnglishPhrase(or something similar),
and it should point to the head of a singly linked list containingthe original English phrase.
The head pointer to the second queue should be named PigLatinPhrase(or something similar),
and it should point to the head of a singly linked list containingthe translated Pig Latin phrase.
You will also need to write a menu-driven routine that will allowusers to test your program.
This menu should allow users to 1) input a phrase in English 2)translate the current phrase from English to Pig Latin 3) printeither the original phrase or the translated phrase to thescreen.
Below is a suggested struct definition for the individual nodes ofyour linked lists:
#define MAX_WORD_CHARS (30)
typedef struct WordNodeType
{
char word[MAX_WORD_CHARS];
struct WordNodeType *next;
}WordNode;
Whatever the definition for your node, will need to implement thefollowing functions;
WordNode* translateWord(WordNode* NextWord)
Precondition: NextWord is passed in as aparameter and points to a WordNode containing and English word.
Postcondition: A pointer to a new node containing the Pig Latintranslation of NextWord is returned to the calling function using areturn statement. NextWord remains unchanged.
void printPhrase(WordNode* Phrase)
Precondition: none
Postcondition: if phrase points to a valid linked list, all thewords contained in the list have been printed in the order thatthey appear in the list. If phrase is a NULL pointer, anappropriate mesage is printed.
Add whatever additional functions and variable you wish toimplement the program.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LINELENGTH 80
#define WORDSEPARATORS ".,:; "
#define WORDSEP " "
int main()
{
int wordlength = 0;
char punctuation;
char* nextword;
char word[25];
char line[80];
char phrase[1000];
// You can use fgets to input a line of up to a specified size
printf("Enter a line of text: ");
fgets(line, 80, stdin); // up to 80 charactersread from stdin
if (strlen(line) <= 1)
printf("Sorry -- I cannot read your text.");
else
{ printf("Here is the line you input: %s", line);
printf("Now let's break up the line intowords! ");
nextword = strtok(line, WORDSEPARATORS);
printf(" Here is the first word in yourphrase: ");
printf("%s ", nextword);
printf(" Here is the second word in yourphrase: ");
nextword = strtok(NULL, WORDSEPARATORS);
printf("%s ", nextword);
}
// Alternatively, you can use gets to grab multiple lines atonce
printf("Enter as many lines as you like: ");
gets(phrase);
if (strlen(phrase) <= 1)
printf("Sorry -- I cannot read your text.");
else
{
printf("Here is your input: ");
printf("%s", phrase);
// Use strtok to separate your larger string into words.
// The first call will use the entire phrase as an argument.
// Getting future words using the same string requires sending NULLas the argument.
// Note that you will need to use a loop to get all words in thephrase.
nextword = strtok(phrase, WORDSEP); // getfirst word using strtok
printf("Here is the first word in yourphrase:");
printf("%s ",nextword);
printf("Here is the second word in yourphrase:");
nextword = strtok(NULL, WORDSEP); // useNULL as parameter for add'l words
printf("%s", nextword);
printf(" ");
}
// strlen tells you where to look for punctuation
wordlength = strlen(nextword);
if (!(isalpha(nextword[wordlength-1])))
{
punctuation = nextword[wordlength-1];
printf("Yourword had the punctuation %c following it! ", punctuation);
}
return 0;
}
Explanation / Answer
Dear... #include "stdafx.h"#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
bool isVowel(char ch);
string rotate(string pStr);
string pigLatinString(string pStr);
int main ()
{ const int LENGTH = 80;
char str [LENGTH];
vowels = ["a", "e","i", "o", "u", "A", "E", "I", "O", "U", "y","Y"] consonants = ["b", “c","d", "f", "g", "h", "j", "k", "l", "m", "n", "p", "q", "r", "s","t", "v", "w", "x", "z", "B", “C", "D", "F", "G", "H", "J","K", "L", "M", "N", "P", "Q", "R", "S", "T", "V", "W", "X","Z"] consonant_string = "" string word, result;
cout << "Enter a string or, blankto exit." <<endl;
cout << "The program will outputthe words in Pig Latin."<<endl;
cin.getline(str,LENGTH);
cout<<endl;
cout << "The pig Latin form of" << str <<" is: "<< pigLatinString(str)<< endl;
return 0;
};
bool isVowel(char ch)
{
switch (ch)
{
case 'A': case 'E':
case 'I': case 'O':
case 'U':
case 'a': case 'e':
case 'i': case 'o':
case 'u':
return true;
default: return false;
};
}
string rotate(string pStr)
{
string::size_typelen=pStr.length();
string rStr;
rStr=pStr.substr(1,len-1)+pStr[0];
return rStr;
}
string pigLatinString(string pStr)
{
string::size_type len;
bool foundVowel;
string::size_type counter;
if (isVowel(pStr[0]))
pStr=pStr+"tay";
else
{
pStr = pStr + "";
pStr=rotate(pStr);
len=pStr.length();
foundVowel=false;
for (counter = 1;counter<len-1; counter++)
if(isVowel(pStr[0]))
{
foundVowel=true;
break;
}
else
pStr=rotate(pStr);
if (!foundVowel)
pStr=pStr.substr(1,len)+"tay";
else
pStr = pStr +"ay";
}
return pStr;
} #include "stdafx.h"
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
bool isVowel(char ch);
string rotate(string pStr);
string pigLatinString(string pStr);
int main ()
{ const int LENGTH = 80;
char str [LENGTH];
vowels = ["a", "e","i", "o", "u", "A", "E", "I", "O", "U", "y","Y"] consonants = ["b", “c","d", "f", "g", "h", "j", "k", "l", "m", "n", "p", "q", "r", "s","t", "v", "w", "x", "z", "B", “C", "D", "F", "G", "H", "J","K", "L", "M", "N", "P", "Q", "R", "S", "T", "V", "W", "X","Z"] consonant_string = "" string word, result;
cout << "Enter a string or, blankto exit." <<endl;
cout << "The program will outputthe words in Pig Latin."<<endl;
cin.getline(str,LENGTH);
cout<<endl;
cout << "The pig Latin form of" << str <<" is: "<< pigLatinString(str)<< endl;
return 0;
};
bool isVowel(char ch)
{
switch (ch)
{
case 'A': case 'E':
case 'I': case 'O':
case 'U':
case 'a': case 'e':
case 'i': case 'o':
case 'u':
return true;
default: return false;
};
}
string rotate(string pStr)
{
string::size_typelen=pStr.length();
string rStr;
rStr=pStr.substr(1,len-1)+pStr[0];
return rStr;
}
string pigLatinString(string pStr)
{
string::size_type len;
bool foundVowel;
string::size_type counter;
if (isVowel(pStr[0]))
pStr=pStr+"tay";
else
{
pStr = pStr + "";
pStr=rotate(pStr);
len=pStr.length();
foundVowel=false;
for (counter = 1;counter<len-1; counter++)
if(isVowel(pStr[0]))
{
foundVowel=true;
break;
}
else
pStr=rotate(pStr);
if (!foundVowel)
pStr=pStr.substr(1,len)+"tay";
else
pStr = pStr +"ay";
}
return pStr;
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.