Question

In: Computer Science

C++ Pig Latin Lab This assignment uses pointers to perform something done often in computer applications:...

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;
}

Solutions

Expert Solution

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:


Related Solutions

C++ Project 6-2: Pig Latin Translator Create a program that translates English to Pig Latin. Console...
C++ Project 6-2: Pig Latin Translator Create a program that translates English to Pig Latin. Console Pig Latin Translator This program translates a sentence and removes all punctuation from it. Enter a sentence: 'Tis but a scratch. Translation:      Istay utbay away atchscray Specifications Convert the English to lowercase before translating. Remove all punctuation characters before translating. Assume that words are separated from each other by a single space. If the word starts with a vowel, just add way to the...
1.This C++ assignment regards pointers and the league with DMA. First part asks to perform tasks...
1.This C++ assignment regards pointers and the league with DMA. First part asks to perform tasks using pointers. The instructions below are a sequence of tasks that are loosely related to each other. Start the assignment by creating a file names pointerTasks.cpp with an empty main function , then add the statements in main() to complete each of the tasks listed below. Some tasks only require a single C++ statement while others will require more. For each step, include a...
IN C++ Note: While there are many ways to do conversions to pig latin, I will...
IN C++ Note: While there are many ways to do conversions to pig latin, I will require that you follow the procedures below, all of which will use the following structure: struct Word { string english; string piglatin; }; Part 1. Write a function that takes in an English sentence as one string. This function should first calculate how many “words” are in the sentence (words being substrings separated by whitespace). It should then allocate a dynamic array of size...
this program is to be done in c language. Using Pointers Create a program pointerTester.c to...
this program is to be done in c language. Using Pointers Create a program pointerTester.c to experiment with pointers. Implement the following steps one by one in your program: YOU NEED TO ANSWER QUESTION Use printf to print your answers at the end(after 12). 1. Declare three integer variables a, b and c. Initialize them to 0, 100 and 225, respectively. 2. Print the value of each variable and its address. 3. Add the following declaration to your code: int...
The times per week a student uses a lab computer are normally​ distributed, with a mean...
The times per week a student uses a lab computer are normally​ distributed, with a mean of 6.5 hours and a standard deviation of 1.5 hours. A student is randomly selected. Find the following probabilities. ​(a) Find the probability that the student uses a lab computer less than 3 hours per week. ​(b) Find the probability that the student uses a lab computer between 6 and 8 hours per week. ​(c) Find the probability that the student uses a lab...
The Lab For this assignment, you need access to a computer running Windows 7, Windows 10...
The Lab For this assignment, you need access to a computer running Windows 7, Windows 10 or a current operating system. Create a folder that contains two subfolders and five files of different types. Each subfolder should have at least three files. Apply each of the NTFS standard permissions to the first folder you created, and allow the permissions to be inherited. Record and report the effects of each change.
The C++ question: This part of the assignment will give you a chance to perform some...
The C++ question: This part of the assignment will give you a chance to perform some simple tasks with pointers. The instructions below are a sequence of tasks that are only loosely related to each other. Start the assignment by creating a file named pointerTasks.cpp with an empty main function, then add statements in main() to accomplish each of the tasks listed below. Some of the tasks will only require a single C++ statement, others will require more than one....
For this computer assignment, you are to write a C++ program to implement a class for...
For this computer assignment, you are to write a C++ program to implement a class for binary trees. To deal with variety of data types, implement this class as a template. Most of the public member functions of the BinaryTree class call private member functions of the class (with the same name). These private member functions can be implemented as either recursive or non-recursive, but clearly, recursive versions of these functions are preferable because of their short and simple implementations...
For this computer assignment, you are to write a C++ program to implement a class for...
For this computer assignment, you are to write a C++ program to implement a class for binary trees. To deal with variety of data types, implement this class as a template. Most of the public member functions of the BinaryTree class call private member functions of the class (with the same name). These private member functions can be implemented as either recursive or non-recursive, but clearly, recursive versions of these functions are preferable because of their short and simple implementations...
For this computer assignment, you are to write and implement an interactive C++ program to find...
For this computer assignment, you are to write and implement an interactive C++ program to find and print all prime numbers, which are less than or equal to a given value of n, using the algorithm known as the Sieve of Eratosthenes. A prime number p is an integer greater than 1 that is divisible only by 1 and p (itself). The algorithm begins by initializing a set container to contain all the integers in the range 2 to n....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT