Hi everyone, I am working on a C++ Pig Latin program for school which translates English sentences into Pig Latin. Here are the guidelines to it:
You are to write a program that translates English to Pig Latin. In case you have forgotten, the way you translate English to Pig Latin is to use the following rules:
- If the word starts with a vowel (A, E, I, O, U), append the string way to the word.
- If the word starts with one or more consonants, move all the consonants preceding the first vowel to the end of the word, and append the string ay.
- If the word starts with qu, move both the q and the u to the end of the word before appending ay.
- If the letter y occurs inside a word, consider it a vowel. If it is the initial letter of a word, consider it a consonant.
- Numbers, punctuation, etc, are passed through unchanged.
I am pretty far along the program but I am having a few issues here. So far I've gotten along to the point where I'm working on if it is a vowel. I have the program able to append 'way', but if it is a sentence it will only append it to the end of the entire sentence, not the end of just the word. An example of this would be if I translated "Anthony is cool." it would become "Anthony is cool.way" Here is my code so far:
#include <iostream>
#include <cstring>
#include <locale>
using namespace std;
void piglatin(char in[], char out[]);
bool vowel(char);
void dovowel(char[], char[], int&, int&);
bool q(char);
void doq(char[], char[], int&, int&);
void doconsonant(char[], char[], int&, int&);
int main()
{
char in[120], out[120];
cout << "Enter a sentence to translate to pig latin (\"exit\" to quit): " << endl;
cin.getline(in,119);
piglatin(in,out);
cout << "The sentence:" << endl;
cout << in << endl;
cout << "Translated into pig latin is:" << endl;
cout << out << endl;
return 0;
}
void piglatin (char in[], char out[])
{
int i=0, j=0;
while (in[i] != '\0')
{
out[i]=in[i];
i++;
}
j=i;
i=0;
while (in[i] != '\0')
{
if (vowel(in[i]))
{
dovowel(in, out, i, j);
break;
}
else if (q(in[i]))
doq(in, out, i, j);
else if (isalpha(in[i]))
doconsonant(in, out, i, j);
else
out[j++] = in[i++];
}
out[j] = '\0';
return;
}
bool vowel(char c)
{
if ((c == 'a') || (c == 'A') || (c == 'e') || (c == 'E') || (c == 'i') || (c == 'I') || (c == 'o') || (c == 'O') || (c == 'u') || (c == 'U'))
return true;
else
return false;
}
void dovowel(char in[], char out[], int &i, int &j)
{
i++;
while (isalpha(in[i]))
{
out[j++] = 'w';
out[j++] = 'a';
out[j++] = 'y';
return;
}
}
bool q(char c)
{
if ((c == 'q') || (c == 'Q'))
return true;
else
return false;
}
void doq(char in[], char out[], int &i, int &j)
{
i++;
return;
}
void doconsonant(char in[], char out[], int &i, int &j)
{
i++;
return;
}