C++ Pig Latin Lab
This assignment uses pointers to perform something done often in computer applications: the parsing of text to find “words” (i.e., strings delineated by some delimiter).
Write a program that encodes English language phrases into Pig Latin. Pig Latin is a form of coded language often used for amusement. Many variations exist in the methods used to form Pig Latin phrases. Use the following algorithm: to form a Pig Latin phrase from an English language phrase, tokenize the phrase into words with the C++ function strtok_s(). To translate each English word into a Pig Latin word, place the first letter of the English word at the end of the English word and add the letters “ay” after it. Thus, the word “jump” becomes “umpjay,” the word “the” becomes “hetay,” and the word “computer” becomes “omputercay.” Blanks between words remain as blanks. Assume that the English phrase input from the keyboard consists of words separated by blanks, there are no punctuations marks, all words have 2 or more letters, and the input phrase is less than 200 characters. Function printLatinWord() should display each word. Hint: Each time a token is found in a call to strtok_s(), pass the token pointer to function printLatinWord() and print the Pig Latin word.
Your program should allow the user to enter phrases until he or she selects an exit option to quit.
In summary: Create a Pig Latin program to implement this functionality: Prompt the user to enter a sentence. Print out the sentence, and then print out the same sentence in Pig Latin. Repeat this sequence until the user elects to quit.
Sol'n so far: (errors in lines 83 and 92)
#include <iostream>
#include <string>
using namespace std;
//class that hold strings of PigLatin
class PigLatin
{
//variable to Piglatin form word
private:
char *latin;
public:
//constructor that converts word into PigLatin
form
PigLatin(char *word)
{
//get the string length
int i, j = 0, len =
strlen(word);
//allocating space
latin = new char[len + 3];
//forming word
for (i = 1; i < len; i++)
{
latin[j] =
word[i];
j++;
}
//Adding last characters
latin[j] = word[0];
j++;
latin[j] = 'a';
j++;
latin[j] = 'y';
j++;
latin[j] = '\0';
}
//Function that returns the word in PigLatin
form
string getLatin()
{
string str(latin);
return str;
}
//Destructor to deallocate memory
~PigLatin()
{
delete[]latin;
}
};
//Function that receives the char * variable as parameter and prints its PigLatin form
void PrintLatinWord(char *str)
{
//creating an object of PigLatin class
PigLatin obj(str);
//Printing word in PigLatin form
cout << obj.getLatin() << " ";
}
//Main function
int main()
{
int i;
char str[200];
char *pch;
char option;
//Loop till user wants to quit
do
{
//Reading a phrase
cout << "\n\n Enter a
sentence to translated:";
cin.getline(str, 200);
//splitting words to
tokens
pch = strtok_s(str, " ");
cout << "\n\t";
//split enter phrase
completes
while (pch != NULL)
{
//Passing
token
PrintLatinWord(pch);
pch =
strtok_s(NULL, " ");
}
//Reading user option
cout << "\n\n Do you want to
enter another sentence? (Y - continue, N - Exit):";
cin.ignore();
} while (option != 'N' && option != 'n');
cout << endl;
system ("pause");
return 0;
}
Description:
In C++, we make use of cstring library to make use of function from C such as strlen() and then we have the function strtoken and strtoken_s(). Please take care of such functions as they will not executing in each and every environment you work.
main.cpp
#include <iostream>
#include <cstring>
using namespace std;
//class that hold strings of PigLatin
class PigLatin
{
//variable to Piglatin form word
private:
char *latin;
public:
//constructor that converts word into PigLatin form
PigLatin(char *word)
{
//get the string length
int i, j = 0, len = strlen(word);
//allocating space
latin = new char[len + 3];
//forming word
for (i = 1; i < len; i++)
{
latin[j] = word[i];
j++;
}
//Adding last characters
latin[j] = word[0];
j++;
latin[j] = 'a';
j++;
latin[j] = 'y';
j++;
latin[j] = '\0';
}
//Function that returns the word in PigLatin form
string getLatin()
{
string str(latin);
return str;
}
//Destructor to deallocate memory
~PigLatin()
{
delete[]latin;
}
};
//Function that receives the char * variable as parameter and prints its PigLatin form
void PrintLatinWord(char *str)
{
//creating an object of PigLatin class
PigLatin obj(str);
//Printing word in PigLatin form
cout << obj.getLatin() << " ";
}
//Main function
int main()
{
int i;
char str[200];
char *pch;
char option;
//Loop till user wants to quit
do
{
//Reading a phrase
cout << "\n\n Enter a sentence to translated:";
cin.getline(str, 200);
//splitting words to tokens
pch = strtok(str, " ");
cout << "\n\t";
//split enter phrase completes
while (pch != NULL)
{
//Passing token
PrintLatinWord(pch);
pch = strtok(NULL, " ");
}
//Reading user option
cout << "\n\n Do you want to enter another sentence? (Y - continue, N - Exit):";
cin >> option;
cin.ignore();
} while (option != 'N' && option != 'n');
cout << endl;
system ("pause");
return 0;
}
Output:
Get Answers For Free
Most questions answered within 1 hours.