A class assignment requires I write a program in C++ that translates phrases into pig latin. It does it a bit differently than most of the pig latin programs I've seen online. Instead of moving the letters up to the first vowel to the end of the word, it moves the first letter of each word to the end of the word and adds "ay".
Example:
English: I SLEPT MOST OF THE NIGHT.
Pig Latin: IAY LEPTSAY OSTMAY FOAY HETAY IGHTNAY.
I've been able to write a program that translates one word into pig latin but I'm not sure how to write this for entire sentences. My assumption would be to use what I've written as a function to read each word in the sentence but I'm not sure how to do that. Any suggestions?
#include <iostream>
#include <string>
using namespace std;
int main()
{
string prepig;
cout << "Enter a phrase and I will translate it to Pig Latin!" << endl;
getline(cin, prepig); // read a whole line into the string name
int index = 0;
char first = ' ';
string remainder;
while (prepig[index] != '\0')
{
if ((!isspace(prepig[index])) && (!ispunct(prepig[index]))) // while != space or punctuation is part of the word
{
first = prepig[0];
index++;
remainder = "";
while ((!isspace(prepig[index])) && (!ispunct(prepig[index])) && (index < prepig.length()))
{
remainder += prepig[index];
index++;
}
remainder += first;
remainder += "-ay";
cout << remainder;
}
}
}
#include <iostream>
#include <string>
usingnamespace std;
void Repeat();
void Repeat1();
void Repeat2();
void Repeat3();
void Repeat4();
//void Repeat5();
//void Repeat6();
//void Repeat7();
//void Repeat8();
//void Repeat9();
int main()
{
string prepig;
cout << "Enter a Word and I will translate it to Pig Latin!\nNo Caps!\nWord: ";
cin>>prepig; /// read a whole line into the string name
int index = 0,YorN=1;
char first = ' ';
string remainder;
while (prepig[index] != '\0')
{
if ((!isspace(prepig[index])) && (!ispunct(prepig[index]))) /// while != space or punctuation is part of the word
{
first = prepig[0];
index++;
remainder = "";
while ((!isspace(prepig[index])) && (!ispunct(prepig[index])) && (index < prepig.length()))
{
remainder += prepig[index];
index++;
}
remainder += first;
remainder += "ay";
cout<<endl<<"Translation: "<< remainder;
}}
cout<<endl<<endl<<"Any more Words?\n1.Yes\n2.No\n> ";\
cin>>YorN;
if(YorN==1)
{
Repeat();
}else{return 0;}
return 0;
}
void Repeat()
{
string prepig;
cout << "Enter a Word and I will translate it to Pig Latin!\nWord: ";
cin>>prepig; /// read a whole line into the string name
int index = 0,YorN=1;
char first = ' ';
string remainder;
while (prepig[index] != '\0')
{
if ((!isspace(prepig[index])) && (!ispunct(prepig[index]))) /// while != space or punctuation is part of the word
{
first = prepig[0];
index++;
remainder = "";
while ((!isspace(prepig[index])) && (!ispunct(prepig[index])) && (index < prepig.length()))
{
remainder += prepig[index];
index++;
}
remainder += first;
remainder += "ay";
cout<<endl<<"Translation: "<< remainder;
}}
cout<<endl<<endl<<"Any more Words?\n1.Yes\n2.No\n> ";\
cin>>YorN;
if(YorN==1)
{
Repeat1();
}
}
void Repeat1()
{
string prepig;
cout << "Enter a Word and I will translate it to Pig Latin!\nWord: ";
cin>>prepig; /// read a whole line into the string name
int index = 0,YorN=1;
char first = ' ';
string remainder;
while (prepig[index] != '\0')
{
if ((!isspace(prepig[index])) && (!ispunct(prepig[index]))) /// while != space or punctuation is part of the word
{
first = prepig[0];
index++;
remainder = "";
while ((!isspace(prepig[index])) && (!ispunct(prepig[index])) && (index < prepig.length()))
{
remainder += prepig[index];
index++;
}
remainder += first;
remainder += "ay";
cout<<endl<<"Translation: "<< remainder;
}}
cout<<endl<<endl<<"Any more Words?\n1.Yes\n2.No\n> ";\
cin>>YorN;
if(YorN==1)
{
Repeat2();
}
}
void Repeat2()
{
string prepig;
cout << "Enter a Word and I will translate it to Pig Latin!\nWord: ";
cin>>prepig; /// read a whole line into the string name
int index = 0,YorN=1;
char first = ' ';
string remainder;
while (prepig[index] != '\0')
{
if ((!isspace(prepig[index])) && (!ispunct(prepig[index]))) /// while != space or punctuation is part of the word
{
first = prepig[0];
index++;
remainder = "";
while ((!isspace(prepig[index])) && (!ispunct(prepig[index])) && (index < prepig.length()))
{
remainder += prepig[index];
index++;
}
remainder += first;
remainder += "ay";
cout<<endl<<"Translation: "<< remainder;
}}
cout<<endl<<endl<<"Any more Words?\n1.Yes\n2.No\n> ";\
cin>>YorN;
if(YorN==1)
{
Repeat3();
}
}
void Repeat3()
{
string prepig;
cout << "Enter a Word and I will translate it to Pig Latin!\nWord: ";
cin>>prepig; /// read a whole line into the string name
int index = 0,YorN=1;
char first = ' ';
string remainder;
while (prepig[index] != '\0')
{
if ((!isspace(prepig[index])) && (!ispunct(prepig[index]))) /// while != space or punctuation is part of the word
{
first = prepig[0];
index++;
remainder = "";
while ((!isspace(prepig[index])) && (!ispunct(prepig[index])) && (index < prepig.length()))
{
remainder += prepig[index];
index++;
}
remainder += first;
remainder += "ay";
cout<<endl<<"Translation: "<< remainder;
}}
cout<<endl<<endl<<"Any more Words?\n1.Yes\n2.No\n> ";\
cin>>YorN;
if(YorN==1)
{
Repeat4();
}
}
void Repeat4()
{
string prepig;
cout << "Enter a Word and I will translate it to Pig Latin!\nWord: ";
cin>>prepig; /// read a whole line into the string name
int index = 0,YorN=1;
char first = ' ';
string remainder;
while (prepig[index] != '\0')
{
if ((!isspace(prepig[index])) && (!ispunct(prepig[index]))) /// while != space or punctuation is part of the word
{
first = prepig[0];
index++;
remainder = "";
while ((!isspace(prepig[index])) && (!ispunct(prepig[index])) && (index < prepig.length()))
{
remainder += prepig[index];
index++;
}
remainder += first;
remainder += "ay";
cout<<endl<<"Translation: "<< remainder;
}}
cout<<endl<<endl<<"Any more Words?\n1.Yes\n2.No\n> ";\
cin>>YorN;
if(YorN==1)
{
cout<<endl<<endl<<"I'm Sorry, there are no more slots left for words. Add to the code for more.";
}
}
You could put all the end words into a vector and output them as a sentence
Here's what I have so far. It isn't exactly what you suggested but it gets the job done. Only it's one word at a time. I'm not sure if you meant entering all the words at once or individually. I'm still not sure how reading them all at once would work.
#include <iostream>
#include <string>
usingnamespace std;
void Repeat();
void Repeat1();
string postpig;
int main()
{
string prepig;
int YorN;
cout << "Would you like for me to translate words to Pig Latin?\n1.Yes\n2.No\n";
cin >> YorN;
while (YorN != 1 && YorN != 2)
{
cout << "Please enter a valid selection:\n";
cout << "Would you like for me to translate words to Pig Latin?\n1.Yes\n2.No\n";
cin >> YorN;
}
if (YorN == 2)
{
cout << "Thank you for checking out my program!";
}
if (YorN == 1)
{
cout << "Enter a sentence, one word at a time, (letters only) and I will translate it to Pig Latin!\nPlease enter the first word and press enter\nWord:";
Repeat();
}
}
void Repeat()
{
string prepig, remainder;
int index = 0, YorN = 1;
char first = ' ';
cin >> prepig;
while (prepig[index] != '\0')
{
if ((!isspace(prepig[index])) && (!ispunct(prepig[index]))) /// while != space or punctuation is part of the word
{
first = prepig[0];
index++;
remainder = "";
while ((!isspace(prepig[index])) && (!ispunct(prepig[index])) && (index < prepig.length()))
{
remainder += prepig[index];
index++;
}
remainder += first;
remainder += "ay";
postpig += " ";
postpig += remainder;
}
}
cout << endl << endl << "Any more Words?\n1.Yes\n2.No\n> "; \
cin >> YorN;
if (YorN == 1)
{
Repeat1();
}
if (YorN == 2)
{
cout << "Here is your sentence:" << postpig << endl << endl;
}
while (YorN != 1 && YorN != 2)
{
cout << "Please enter a valid selection:\n";
cout << "Would you like for me to translate words to Pig Latin?\n1.Yes\n2.No\n";
cin >> YorN;
}
}
void Repeat1()
{
string prepig, remainder;
int index = 0, YorN = 1;
char first = ' ';
cout << "Enter the next word in the sentence and press enter\nWord: ";
cin >> prepig;
while (prepig[index] != '\0')
{
if ((!isspace(prepig[index])) && (!ispunct(prepig[index]))) /// while != space or punctuation is part of the word
{
first = prepig[0];
index++;
remainder = "";
while ((!isspace(prepig[index])) && (!ispunct(prepig[index])) && (index < prepig.length()))
{
remainder += prepig[index];
index++;
}
remainder += first;
remainder += "ay";
postpig += " ";
postpig += remainder;
}
}
cout << endl << endl << "Any more Words?\n1.Yes\n2.No\n> "; \
cin >> YorN;
if (YorN == 1)
{
Repeat1();
}
if (YorN == 2)
{
cout << "Here is your sentence:" << postpig << endl << endl;
}
while (YorN != 1 && YorN != 2)
{
cout << "Please enter a valid selection:\n";
cout << "Would you like for me to translate words to Pig Latin?\n1.Yes\n2.No\n";
cin >> YorN;
}
}
#include <vector>
#include <iostream>
#include <algorithm>
#include <string>
#include <iterator>
usingnamespace std;
void Repeat();
void Repeat1();
void Repeat2();
void Repeat3();
void Repeat4();
void Repeat5();
void Repeat6();
void Repeat7();
void Repeat8();
void Repeat9();
void sentence();
string addWord, removeWord;
vector<string> WordList;
vector<string>::iterator WordIter;
int numberOfWords = WordList.size();
int main()
{
string prepig;
cout << "Enter a Word and I will translate it to Pig Latin!\nNo Caps!\nWord: ";
cin>>prepig; /// read a whole line into the string name
int index = 0,YorN;
char first = ' ';
string remainder;
while (prepig[index] != '\0')
{
if ((!isspace(prepig[index])) && (!ispunct(prepig[index]))) /// while != space or punctuation is part of the word
{
first = prepig[0];
index++;
remainder = "";
while ((!isspace(prepig[index])) && (!ispunct(prepig[index])) && (index < prepig.length()))
{
remainder += prepig[index];
index++;
}
remainder += first;
remainder += "ay";
cout<<endl<<"Translation: "<< remainder;
addWord=remainder;
WordList.push_back(addWord);
}}
cout<<endl<<endl<<"Any more Words?\n1.Yes\n2.No\n> ";\
cin>>YorN;
if(YorN==1)
{
Repeat();
}else{return 0;}
return 0;
}
void Repeat()
{
string prepig;
cout << "Enter a Word and I will translate it to Pig Latin!\nWord: ";
cin>>prepig; /// read a whole line into the string name
int index = 0,YorN=1;
char first = ' ';
string remainder;
while (prepig[index] != '\0')
{
if ((!isspace(prepig[index])) && (!ispunct(prepig[index]))) /// while != space or punctuation is part of the word
{
first = prepig[0];
index++;
remainder = "";
while ((!isspace(prepig[index])) && (!ispunct(prepig[index])) && (index < prepig.length()))
{
remainder += prepig[index];
index++;
}
remainder += first;
remainder += "ay";
cout<<endl<<"Translation: "<< remainder;
addWord=remainder;
WordList.push_back(addWord);
}}
cout<<endl<<endl<<"Any more Words?\n1.Yes\n2.No\n> ";\
cin>>YorN;
if(YorN==1)
{
Repeat1();
}
}
void Repeat1()
{
string prepig;
cout << "Enter a Word and I will translate it to Pig Latin!\nWord: ";
cin>>prepig; /// read a whole line into the string name
int index = 0,YorN=1;
char first = ' ';
string remainder;
while (prepig[index] != '\0')
{
if ((!isspace(prepig[index])) && (!ispunct(prepig[index]))) /// while != space or punctuation is part of the word
{
first = prepig[0];
index++;
remainder = "";
while ((!isspace(prepig[index])) && (!ispunct(prepig[index])) && (index < prepig.length()))
{
remainder += prepig[index];
index++;
}
remainder += first;
remainder += "ay";
cout<<endl<<"Translation: "<< remainder;
addWord=remainder;
WordList.push_back(addWord);
}}
cout<<endl<<endl<<"Any more Words?\n1.Yes\n2.No\n> ";\
cin>>YorN;
if(YorN==1)
{
Repeat2();
}
}
void Repeat2()
{
string prepig;
cout << "Enter a Word and I will translate it to Pig Latin!\nWord: ";
cin>>prepig; /// read a whole line into the string name
int index = 0,YorN=1;
char first = ' ';
string remainder;
while (prepig[index] != '\0')
{
if ((!isspace(prepig[index])) && (!ispunct(prepig[index]))) /// while != space or punctuation is part of the word
{
first = prepig[0];
index++;
remainder = "";
while ((!isspace(prepig[index])) && (!ispunct(prepig[index])) && (index < prepig.length()))
{
remainder += prepig[index];
index++;
}
remainder += first;
remainder += "ay";
cout<<endl<<"Translation: "<< remainder;
addWord=remainder;
WordList.push_back(addWord);
}}
cout<<endl<<endl<<"Any more Words?\n1.Yes\n2.No\n> ";\
cin>>YorN;
if(YorN==1)
{
Repeat3();
}
}
void Repeat3()
{
string prepig;
cout << "Enter a Word and I will translate it to Pig Latin!\nWord: ";
cin>>prepig; /// read a whole line into the string name
int index = 0,YorN=1;
char first = ' ';
string remainder;
while (prepig[index] != '\0')
{
if ((!isspace(prepig[index])) && (!ispunct(prepig[index]))) /// while != space or punctuation is part of the word
{
first = prepig[0];
index++;
remainder = "";
while ((!isspace(prepig[index])) && (!ispunct(prepig[index])) && (index < prepig.length()))
{
remainder += prepig[index];
index++;
}
remainder += first;
remainder += "ay";
cout<<endl<<"Translation: "<< remainder;
addWord=remainder;
WordList.push_back(addWord);
}}
cout<<endl<<endl<<"Any more Words?\n1.Yes\n2.No\n> ";\
cin>>YorN;
if(YorN==1)
{
Repeat4();
}
}
void Repeat4()
{
string prepig;
cout << "Enter a Word and I will translate it to Pig Latin!\nWord: ";
cin>>prepig; /// read a whole line into the string name
int index = 0,YorN=1;
char first = ' ';
string remainder;
while (prepig[index] != '\0')
{
if ((!isspace(prepig[index])) && (!ispunct(prepig[index]))) /// while != space or punctuation is part of the word
{
first = prepig[0];
index++;
remainder = "";
while ((!isspace(prepig[index])) && (!ispunct(prepig[index])) && (index < prepig.length()))
{
remainder += prepig[index];
index++;
}
remainder += first;
remainder += "ay";
cout<<endl<<"Translation: "<< remainder;
addWord=remainder;
WordList.push_back(addWord);
}}
cout<<endl<<endl<<"Any more Words?\n1.Yes\n2.No\n> ";\
cin>>YorN;
if(YorN==1)
{Repeat5();}
}
void Repeat5()
{
string prepig;
cout << "Enter a Word and I will translate it to Pig Latin!\nWord: ";
cin>>prepig; /// read a whole line into the string name
int index = 0,YorN=1;
char first = ' ';
string remainder;
while (prepig[index] != '\0')
{
if ((!isspace(prepig[index])) && (!ispunct(prepig[index]))) /// while != space or punctuation is part of the word
{
first = prepig[0];
index++;
remainder = "";
while ((!isspace(prepig[index])) && (!ispunct(prepig[index])) && (index < prepig.length()))
{
remainder += prepig[index];
index++;
}
remainder += first;
remainder += "ay";
cout<<endl<<"Translation: "<< remainder;
addWord=remainder;
WordList.push_back(addWord);
}}
cout<<endl<<endl<<"Any more Words?\n1.Yes\n2.No\n> ";\
cin>>YorN;
if(YorN==1)
{
sentence();
}
}
void sentence()
{
numberOfWords = WordList.size();
cout << "\nYou have " << numberOfWords << " Words in your Sentence.\n" << endl;
for (WordIter = WordList.begin(); WordIter != WordList.end(); ++WordIter) {
cout<<" "<<*WordIter;
}
cout << "\n";
}
#include <iostream>
#include <string>
#include <sstream>
#include <algorithm>
#include <cctype>
int main()
{
std::cout << "Please etnter a sentence: " << std::flush;
std::string sentence;
std::getline(std::cin, sentence);
std::stringstream ss(sentence);
std::string word;
while (ss >> word)
{
//remove non alphabetic characters
word.erase(std::remove_if(word.begin(), word.end(), [](char inChar) { return !isalpha(inChar); }), word.end());
//move first character in word to the end of the word
word.push_back(word.front());
word.erase(word.begin());
//make the entire word uppercase
std::transform(word.begin(), word.end(), word.begin(), toupper);
//print word
std::cout << word << "AY ";
}
std::cin.ignore(10000, '\n');
return 0;
}