In: Computer Science
C++
Project 6-2: Pig Latin Translator
Create a program that translates English to Pig Latin.
Console
Pig Latin Translator
This program translates a sentence
and removes all punctuation from it.
Enter a sentence: 'Tis but a scratch.
Translation: Istay utbay away atchscray
Specifications
Note
`Hey,
Note: Brother if you have any queries related the answer please do comment. I would be very happy to resolve all your queries.
#include <string>
#include <iostream>
#include<cstdlib>
using namespace std;
int flag=1;
///////////////////////////////////////////////////////////////////////
///////////////////////FUNCTION
LIST///////////////////////////////////////////////
bool isVowels(char x);
void Vowel(string changed);
void nonVowel(char begin, string end);
void changeLine (string line);
void printHeader();
////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////
////////////////////BEGIN THE MAIN
PROGRAM///////////////////////////////////////////
int main()
{
char ch;
string word, first, line;
getline(cin,line);//input a line of words
cout <<endl;
string nLine="";
for(int i=0;i<line.length();i++)
{
if(!ispunct(line[i]))
nLine=nLine+line[i];
}
changeLine(nLine);// output of the new sentence in Pig Latin
return 0;
}
/////////////////////////////////////////////////////////////////////////////////
////////////////////////////END OF
MAIN//////////////////////////////////////////
bool isVowels(char x) // this is where all of the vowels in the
words are found. It will
//read in the word, change the letter to loower case.
{
char low_case = tolower (x);
if (low_case == 'a' )
return true;
else if (low_case == 'e' )
return true;
else if ( low_case== 'i' )
return true;
else if (low_case == 'o' )
return true;
else if (low_case == 'u' )
return true;
else
return false;
}
void changeLine (string line)//this is where the strings are
changed. As long as there is
//not a Null character, changeling will look at each word. It will
look for the blank spaces
//between the words. One a word is found it is the look at to
whether it has a vowel in it or not
// then it is rearranged into a string.
{
string word, first;
char ch;
while (line.length() !=0)//line consist of characters
{
int wordLen = line.find(' ');
if (wordLen >0)
{
word = line.substr(0,wordLen);
first=word.substr(0,1);
ch = first[0];
string notFirst(word,1);
if (isVowels (ch))// vowel found
{
Vowel(word);
}
else
{
nonVowel(ch,notFirst);//no vowel found
}
line = line.substr(wordLen+1);
}
else
{
word = line;
ch = line[0];
string notFirst(word,1);
if (isVowels (ch))
Vowel(word);
else
nonVowel(ch, notFirst);
line="";
}
}
}
void Vowel(string changed)//what this does is when the string of
the new word is brought in
//it is changed due to Pig latin configuration for words that begin
with a vowel and then added "way" to the end of the word.
//this new word is then processed out to the string to put back to
sentence form.
{
string newWord;
newWord=changed+"way";
for(int i=0;i<newWord.length();i++)
{
if(flag==0)
{
if(newWord[i]>='A'&&newWord[i]<='Z')
cout<<(char)(newWord[i]-'A'+'a');
else
cout<<newWord[i];
}
else
{
if(newWord[i]>='a'&&newWord[i]<='z')
cout<<(char)(newWord[i]-'a'+'A');
flag=0;
}
}
cout<<" ";
}
void nonVowel(char begin, string end)//what this does is when
the string of the new word is brought in
//it is changed due to Pig latin configuration for words that do
not begin
//with a vowel and then added "ay" to the end of the word.this new
word is then processed
//out to the string to put back to sentence form.
{
string newWord;
newWord=end+begin;
if(!isVowels(newWord[0]))
{
string temp(newWord,1);
nonVowel(newWord[0],temp);
}
else
{
for(int i=0;i<newWord.length();i++)
{
if(flag==0)
{
if(newWord[i]>='A'&&newWord[i]<='Z')
cout<<(char)(newWord[i]-'A'+'a');
else
cout<<newWord[i];
}
else
{
if(newWord[i]>='a'&&newWord[i]<='z')
cout<<(char)(newWord[i]-'a'+'A');
flag=0;
}
}
cout<<"ay ";
}
}
Kindly revert for any queries
Thanks.