Write a program that reads a sentence as input and converts each word to “Pig Latin.” To convert an English 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 of the conversion to Pig Latin:
I need help with the code that I have written. I am getting errors with 'int i' in void pigLatin(). Please need your feedback and if I need to make any changes, please tell me. Appreciated your help.
#include <iostream>
#include <string>
using namespace std;
void pigLatin();
string PLW(string); //PLW for "Pig Latin Wod"
int main ()
{
cout << " Pig Latin Translator " << endl;
cout << "-----------------------------\n" << endl;
}
void pigLatin()
{
string entry= " ", PLatin= " ";
string entSent= " ";
cout <<" Please enter a sentence below! \n" << "English: " << endl;
getline(cin, entry);
int i = entry.find (' ');
while ( i < entry.length()-1)
{
entSent = entry.substr(0, i);
entry = entry.substr(i + 1); //takes the 1st letter
PLatin += PLW(entSent)+ " "; //translates the word and adds to collected pig latin phrase
i = entry.find(' '); //find next space
}
PLatin+=PLW(entry);
cout << "\n";
cout << "Pig Latin: " <<PLatin;
}
string PLW (string r) // accepts word in English, returns word in pig latin
{
string ans= " ", prefix= " ";
for (int j=0; j < r.length(); j++)
{
ans = r.substr(prefix.length());
ans+= prefix + "ay";
}
return ans;
}