Pig Latin Write a program that reads a sentence as input and
Pig Latin Write a program that reads a sentence as input and converts each word to “Pig Latin.” In one version, to convert a word to Pig Latin you remove the first letter and place that letter at the end of the word. Then you append the string “ay” to the word. Here is an example: English: I SLEPT MOST OF THE NIGHT Pig Latin: IAY LEPTSAY OSTMAY FOAY HETAY IGHTNAY
This has to be in C++
Any help would be greatly appricated, thanks in advance.
Solution
/*
C ++ program that prompt to enter a string
and press enter and then converts the string
to pig latin and print the result to console
*/
#include <iostream>
#include <string>
using namespace std;
//function prototype
string toPigLatin(string);
//set constant
const int size = 100;
char str[size];
int main()
{
cout << \"Enter a string message: \";
//read a c-string
cin.getline (str,size);
//calling toPiglatin
cout<<\"Pig Latin : \"<<toPigLatin(str)<<endl;
system(\"pause\");
return 0;
}
/**
The function toPigLatin that takes a string str
and converts the string to pig latin word
and returns to calling program
*/
string toPigLatin(string str)
{
str =str+\" \";
int start = 0;
int begin = 0;
string word;
string pigLatin = \"\";
//continue until find returns invalid for space in strTran
while(true)
{
start = str.find(\' \', start);
if(start == string::npos)
break;
word = str.substr(begin, start - begin);
//append \"AY\" to end of the with starting word and remainng and last \"AY\"
pigLatin += word.substr(1) + word.substr(0, 1) + \"AY \";
//increment start by 1
start++;
//set start t begin
begin = start;
}
return pigLatin;
}
--------------------------------------------------------------
Sample output:
Enter a string message: I SLEPT MOST OF THE NIGHT
Pig Latin : IAY LEPTSAY OSTMAY FOAY HETAY IGHTNAY

