Question: This program reads a sentence as input and coverts it into Pig Latin. It takes the first letter and places it at the end of
This program reads a sentence as input and coverts it into Pig Latin. It takes the first letter and places it at the end of each word in the sentence. Then it will append an ay to the end of that word. So for example, the word slept will be converted to lepts, and then appended with the ay to form the Pig Latin version, leptsay. Single letter words are easy, such as I, which becomes Iay.
Your task is to write an optimized algorithm (in main below) that converts a sentence into Pig Latin in as few statements as possible (in the box). Use the program's given constructs and variables only. Unless it is a loop variable, any added variables are 2 points. An instruction is interchangeable with a statement. Write the C++ statements that complete the task. Hint: Use any string methods to reduce your code...
#include #include #include
using namespace std;
// Prototypes int main()
{
string sentence, word, piglatin; piglatin = ""; cout<<"Please enter your sentence: ";
getline(cin,sentence); int index = 0;
// YOUR CODE HERE...
// Translate each word and create a new string
// Get the next word from the string
// Translate the word to Pig Latin
// Add the word and a space
// Display the translated sentence return 0;}
// Erase 1 character at index 0
string popWord(string &sentence) {
// Trim any spaces
// Locate the first space // Copy the substring-index, and store the copy in word
// Remove the word from sentence
// Return the extracted word
string toPigLatin(string word) {
// Get the first letter of the word
// Append the letter
// Append "ay" to the word
// Delete the first letter
// Return the word
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
