In C++, Write a function that converts a string to its Pig-latin equivalent. To
ID: 3779007 • Letter: I
Question
In C++, Write a function that converts a string to its Pig-latin equivalent. To convert a string to Pig-latin use the following definition of Pig-latin:
1. If the string begins with a vowel, add "way" to the string. For example, Pig-latin for "orange" is "orangeway".
2. Otherwise, find the first occurrence of a vowel, move all the characters before the vowel to the end of the word, and add "ay". For example, Pig-latin for "story" is "orystay" since the characters "st" occur before the first vowel.
3. If there are no vowels, add "ay", so Pig-latin for "RPM" is "RPMay".
Explanation / Answer
#include <iostream>
#include <string>
using namespace std;
string Piglatin(string input_String);
int main()
{
char english[100];
cout << "Enter the string:"<< endl;
cin>> english;
//string english = "RPM";
cout << english << " converted to pig latin is " << Piglatin(english) << endl;
cin.ignore();
return 0;
}
string Piglatin(string input_String)
{
string add = "ay";
string vowel = "aeiouAEIOU";
size_t vowel_Index;
string stringTemp = "";
string firstLetter = "";
firstLetter = input_String.front();
//checks to see if the first letter is a vowel
if (vowel.substr(0, 9).find(firstLetter) != std::string::npos)
{
add = "way";
}
else
{
vowel_Index = input_String.find_first_of(vowel);
stringTemp = input_String.substr(0, vowel_Index);
input_String = input_String.substr(vowel_Index, input_String.size() - 1);
}
return string(input_String + stringTemp + add);
}