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;
}

Recommended Answers

All 3 Replies

I'll show you what I'd do, maybe it will give ye' some ideas on how to handle ye' problem:

1. Get a sentence to translate from the user

#include<sstream>
#include<string>
#include<iostream>
#include<cctype>

cout << "Enter sentence to translate to pig latin: ";
cin >> user_input;

2. Parse the sentence into individual substrings

stringstream sentence;
string words[20];
int word_counter = 0;

//load the stringstream object with the sentence
sentence << user_input;

while(sentence)
{
     words[word_counter] << sentence;   //Parsing is 'white space' delimited
     word_counter++;
}

3. Test each word and handle it accordingly

int i=0;

while(i < word_counter)
{
     if(vowel(words[i][0]))   //Test first letter of each word
     {
          words[i] += "way";  //Append string to end of word
     }
   
     /*
          Perform all the other tests whilst inside this loop
          
          Modify each word as necessary according to pig latin rules
     */

     i++;  //Increment to next word
}

opefullyhay histay elpshay.

I want to write a coding which is to trnslate english sentence into other language sentence. For example: in english "i love u" and iny other language. "Wo ai ni" pliss help me.

First off, do not commit thread necromancy; this thread had been dead for four years, until you ressurected it. If you have a new question, start a new thread.

Second, please avoid textspeak or other types of online abbreviations in this forum. Write clear, concise and grammatically correct English if you want people here to take you seriously.

Third, creating a Pig Latin encoder is a very different prospect from a natural language translator, so this isn't really the right thread for this in any case.

Finally, the kind of translator you are talking about is far, far beyond what even a talented individual programmer could achieve in a reasonable time frame; translators such as the Babelfish engine are the product of dozens if not hundreds of coders working for years, and even then they are invariably very limited in usefulness. Creating a truly effective translator is on open research problem, and may - not definitely, but nonetheless probably - require solving the Hard AI Problem to really make it work (that is, it may be only possible for a sentient program with human-equivalent or higher intelligence to translate the nuances of one natural language into another correctly and accurately).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.