/* Pig Latin - This program reads a sentence as input and converts each
word to "Pig Latin." To convert a word to Pig Latin, the first letter
is removed and placed at the end of the word, then the string "ay" is
appended to the word. For example:
* English: I SLEPT MOST OF THE NIGHT
* Pig Latin: IAY LEPTSAY OSTMAY FOAY HETAY IGHTNAY */
#include "Utility.h"
/* Asks the user if he or she wants to repeat the process,
returns the decision */
char tryAgain();
/* Converts each word to pig latin,
returns the words */
string pigLatin(string);
/* Converts each word to uppercase */
void wordCapitalizer(string &);
/* Removes punctuation from a sentence */
void removePunct(string &);
/* Clears the input of the three string object */
void clearInput(string &, string &, string &);
int main()
{
char again = ' ';
string word;
string pigSentence;
string englishSentence;
cout << "\tPIG LATIN TRANSLATOR\n\n"
<< "\tEnter a sentence, and I will translate it for you "
<< "from English to Pig Latin:\n\t";
do
{
while (cin >> word)
{
englishSentence.append(word + " ");
removePunct(word);
wordCapitalizer(word);
pigSentence.append(pigLatin(word));
if (cin.get() == '\n')
{
cout << "\n\tThis was your original sentence:\n\t"
<< englishSentence << "\n";
cout << "\n\tThis is your sentence in Pig Latin:\n\t"
<< pigSentence;
break;
}
}
again = tryAgain();
if (again == 'Y')
{
cout << "\n\tPlease enter another sentence:\n\t";
clearInput(word, pigSentence, englishSentence);
}
else
{
cout << "\tHave a nice day!\n\n";
}
} while (again == 'Y');
pauseSystem();
return 0;
}
/* **********************************************************
Definition: pigLatin
This function stores the first letter of each word in a
variable, then the first letter is erased, and in a final
step the letter plus "AY " is appended to the word.
********************************************************** */
string pigLatin(string word)
{
string tmp = word.substr(0, 1);
string pLatin = "AY ";
word.erase(word.begin());
word.append(tmp + pLatin);
return word;
}
/* **********************************************************
Definition: removePunct
This function erases all punctuation from the string.
********************************************************** */
void removePunct(string &sentence)
{
for (unsigned int i = 0; i < sentence.length(); i++)
{
if (ispunct(sentence.at(i)))
{
sentence.erase(i++, 1);
}
}
}
/* **********************************************************
Definition: wordCapitalizer
This function converts a word to uppercase.
********************************************************** */
void wordCapitalizer(string &word)
{
for (unsigned int index = 0; index < word.length(); index++)
{
if (isalpha(word.at(index)))
{
word.at(index) = toupper(word.at(index));
}
}
}
/* **********************************************************
Definition: tryAgain
This function asks the user if he or she wishes to try
again. This decision is returned.
********************************************************** */
char tryAgain()
{
char again = ' ';
cout << "\n\n\tDo you wish to try this again? ";
cin >> again;
cin.ignore();
/* Input validation */
while (toupper(again) != 'Y' && toupper(again) != 'N')
{
cout << "\n\tDo you wish to try this again? ";
cin >> again;
}
return toupper(again);
}
/* **********************************************************
Definition: clearInput
This function clears the contents in the string objects.
********************************************************** */
void clearInput(string &word, string &pigSentence,
string &englishSentence)
{
word.clear();
pigSentence.clear();
englishSentence.clear();
}
Thursday, May 4, 2017
Programming Challenge 10.16 - Pig Latin
Subscribe to:
Post Comments (Atom)
put the link
ReplyDeleteHow about much more simpler way to code pig Latin with C++
ReplyDelete