Question

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

Homework Answers

Answer #1

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:

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
The code is in C programming language pls convert it into python. Thanks. Program --> #include...
The code is in C programming language pls convert it into python. Thanks. Program --> #include <stdio.h> #include <stdlib.h> void main() { //declare variables FILE *fileptr; char filename[15]; char charRead; char filedata[200],searchString[50]; int i=0,j=0,countNoOfWord=0,count=0; //enter the filename to be opened printf("Enter the filename to be opened \n"); scanf("%s", filename); /* open the file for reading */ fileptr = fopen(filename, "r"); //check file exit if (fileptr == NULL) { printf("Cannot open file \n"); exit(0); } charRead = fgetc(fileptr); //read the string...
JAVA ASSIGNMENT 1. Write program that opens the file and process its contents. Each lines in...
JAVA ASSIGNMENT 1. Write program that opens the file and process its contents. Each lines in the file contains seven numbers,which are the sales number for one week. The numbers are separated by comma.The following line is an example from the file 2541.36,2965.88,1965.32,1845.23,7021.11,9652.74,1469.36. The program should display the following: . The total sales for each week . The average daily sales for each week . The total sales for all of the weeks .The average weekly sales .The week number...
can you please do this lab? use lunix or C program its a continuation of a...
can you please do this lab? use lunix or C program its a continuation of a previous lab. the previous lab: Unix lab 4: compile and link multiple c or c++ files Please do the following tasks step by step: create a new directory named by inlab4 enter directory inlab4 create a new file named by reverse.c with the following contents and then close the file: /*reverse.c */ #include <stdio.h> reverse(char *before, char *after); main() {       char str[100];    /*Buffer...
For a C program hangman game: Create the function int setup_game [int setup_game ( Game *g,...
For a C program hangman game: Create the function int setup_game [int setup_game ( Game *g, char wordlist[][MAX_WORD_LENGTH], int numwords)] for a C program hangman game. (The existing code for other functions and the program is below, along with what the function needs to do) What int setup_game needs to do setup_game() does exactly what the name suggests. It sets up a new game of hangman. This means that it picks a random word from the supplied wordlist array and...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT