Write a program in C++ that will input a phrase and convert it to pig latin. Put
ID: 3665965 • Letter: W
Question
Write a program in C++ that will input a phrase and convert it to pig latin. Put each word in a separate element of a string array. Remove the first letter from each word and concatenate it to the end of the word followed by “ay.”
Sample Output of Program:
*****************************************************
* You will be prompted to enter a string of *
* words. The string will be converted into *
* Pig Latin and the results displayed. *
* Enter as many strings as you would like. *
******************************************************
Enter a group of words or ENTER to quit: Computer Programming is fun to learn!
Original words: Computer Programming is fun to learn!
New Words: omputercay ogrammingpray isway unfay otay earnlay!
Enter a group of words or ENTER to quit: Quit
Explanation / Answer
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
using namespace std;
void ParseString( const string& aLine, vector< string>& tokens )
{
string buffer;
stringstream ss( aLine);
while( ss >> buffer )
tokens.push_back( buffer );
}
int main( int argc, char** argv )
{
bool bFoundVowel = false;
int
i = 0,
j = 0;
string
message = "",
new_word = "",
pig_message = "",
play_again = "Y",
vowels = "aeiou";
vector< string > Tokens;
while( play_again == "Y" || play_again == "y" )
{
pig_message = "";
Tokens.clear();
cout << "Input Word or phrase: ";
getline( cin, message );
ParseString( message, Tokens );
vector< string >::iterator itr;
for( i = 0, itr = Tokens.begin(); itr != Tokens.end(); i++, itr++ )
{
string AWord = *itr;
bFoundVowel = false;
if( AWord.length() < 2 )
{
cerr << "[" << AWord << "] is too short!" << endl;
continue;
}
for( j = 0; j < vowels.length(); j++ )
{
if( tolower( AWord[ 0 ] ) == vowels[ j ] )
{
bFoundVowel = true;
new_word = AWord + "ay ";
pig_message += new_word;
break;
}
}
if( ! bFoundVowel )
{
new_word = AWord.substr( 1 );
new_word += AWord[ 0 ];
new_word += "ay ";
pig_message += new_word;
}
}
cout << pig_message << endl;
cout << "Would you like to play again? (Y or N) ";
getline( cin, play_again );
}
cout << "IIIII! (Press Enter to exit)";
return 0;
}
2nd way:
#include <iostream>
#include <string>
#include <cctype>
#include "console.h"
#include "simpio.h"
#define SENTINEL 0
using namespace std;
string lineToPigLatin(string line);
string wordToPigLatin(string word);
int findFirstVowel(string word);
bool isVowel(char ch);
string lineToPigLatin(string line)
{
string result;
int start = -1;
for ( int i=0; i < line.length(); ++i )
{
char ch = line[i];
if ( isalpha(ch) )
{
if ( start == -1 ) start = i;
}
else
{
if ( start >= 0 )
{
result += wordToPigLatin(line.substr(start, i-start));
start = -1;
}
result += ch;
}
}
if ( start >= 0 ) result += wordToPigLatin(line.substr(start));
return result;
}
string wordToPigLatin(string word)
{
int vp = findFirstVowel(word);
if ( vp == -1 )
{
return word;
}
else if ( vp == 0 )
{
return word + "way";
}
else
{
string head = word.substr(0,vp);
string tail = word.substr(vp);
return tail + head + "ay";
}
}
int findFirstVowel(string word)
{
for ( int i=0; i < word.length(); ++i )
{
if ( isVowel(word[i]) ) return i;
}
return -1;
}
bool isVowel(char ch)
{
switch ( ch )
{
case 'A': case 'E': case 'I': case 'O': case 'U':
case 'a': case 'e': case 'i': case 'o': case'u':
return true;
default:
return false;
}
}
int main()
{
string line, translation;
while ( true )
{
line = getLine("Enter English text: ");
if ( line == "" ) break;
translation = lineToPigLatin(line);
cout << "Pig Latin output: " << translation << endl;
cout << endl;
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.