Pig Latin Translator

Oct 8, 2019 at 3:19am
Can someone help me on where to start with this prompt?? I know I'll need strings, a loop, and some if statements but if someone could get the code started for me that would be really appreciated!!

Pig Latin Translator

Write a program that prompts a user for a sentence. (Each word in the sentence should be separated by a single space. The sentence should not contain punctuation.) Your program will then convert each word to Pig Latin and print out the translated sentence. After the sentence is successfully translated, the program should ask if the user would like another sentence translated. If the user does, the program should loop. If not the program should end.

Pig Latin Rules

There are many different pig latin rules and variations, but listed here are the rules you need to implement.
1. If a word begins with a vowel, such as "apple" , "-yay" should be appended to the end of the word. "apple-yay"
2. If a word begins with a single consonant, such as "barbecue" , the consonant will be moved from the beginning of the word to end with an additional "ay" , "arbecue-bay"
3. If a word begins with a two letter or three letter blend, such as "blend" or "splendid" , the letters that make up the blend are moved to the end of the word with an additional "ay" "ended-blay" and "endid-splay"
4. Y's can be tricky. If they begin a word, treat them as a consonant otherwise as a vowel.

Note: Make sure to handle if the first letter of a word is capitalized. Remember you can use the "tolower()" function to convert a char to its lowercase conterpart. Ex: tolower('C') becomes 'c'

If someone could help me out that would be great!! Thanks!
Oct 8, 2019 at 4:46am
If a start is all you want, then
1
2
int main ( ) {
}


But it seems to me like you want the whole meal prepared for you in advance.

Oct 8, 2019 at 1:47pm
Start with the data. You're prompting the user for a sentence but most of the work is done with individual words. So You'll want to store the sentence as a vector of words.

Next, read the problem slowly and start sketching out in your editor the parts that you'll need:

Write a program that prompts a user for a sentence. (Each word in the sentence should be separated by a single space. The sentence should not contain punctuation.)

That sounds like a function:
vector<string> getSentence();

Your program will then convert each word to Pig Latin
We can do this word-by-word:
1
2
3
4
for each word in the sentence {
    newWord = translate(word);
    store newWord in the translated sentence
}


...and print out the translated sentence
1
2
3
for each word in the translated sentence {
    print the word and a space
}


After the sentence is successfully translated, the program should ask if the user would like another sentence translated. If the user does, the program should loop. If not the program should end.

Oh, so we need all that stuff above in a loop and at the bottom it needs to prompt:

1
2
3
4
5
6
do {
   all the stuff above

   cout << "Do you want to do another sentence? ";
   cin >> answer;
} while (answer == "yes");


Pig Latin Rules

Here come a whole bunch of rules. Fortunately, we decided to use a function to translate a single word, so all of the rules apply to our translate() function.

Now here's a trick: write the program, but start with a stub for the translate function. The stub doesn't translate anything, it just returns the original string:

1
2
3
4
string translate(const string &word)
{
   return word;
}


Get the whole program working with this stub. Once all the other stuff is working, you can concentrate on writing the real translate() function.

Write code to implement each of the rules. If you're clever, you'll see that the first 3 rules can be expressed as a single rule instead.

Oh, it talks about consonants and vowels. Better write a little helper routine to tell if a character is a vowel:
bool isVowel(char ch)

Now go for it. If you get stuck for more than an hour, post whatever code you have and explain the problem that you're having.

Good luck!
Dave
Oct 10, 2019 at 9:26pm
#include <iostream>
#include <vector>
#include <string>

int main()
{
const int stringLength = 50;

cout << "Welcome to Pig Latin Translator! << "Enter in a sentence to be translated" endl;
cin >> answer;

for each word in the sentence{
newWord = translate(word);
store newWord in the translated sentence
}
for each word in the translated sentence{
print the word and a space
}

cout << "Do you want to do another sentence? ";
cin >> answer;
while (answer == "yes");

string translate(const string & word)
{
return word;

if stringLength

This is what I have so far. There are already many errors and this project is due very soon. I don't know how to fix the errors and what to do for the if statements!!! Help!?!?!
Oct 11, 2019 at 2:06pm
Here is a modified version of your code. This just reads a line of text, parses it into individual words and prints out the words. I have commented out most of your code by surrounding it by #if 0 and #endif This tells the compiler to ignore everything in between.

Most of the "code" in my previous post was just pseudo-code, not actual C++. The idea was for you to translate the pseudo-code into C++.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <sstream>
#include <vector>
#include <string>

using namespace std;

int main()
{
    const int stringLength = 50;
    string line;		// the input line
    string word;
    vector<string> sentence;	// line broken into words
    cout << "Welcome to Pig Latin Translator!\n"
	 << "Enter in a sentence to be translated"
	 << endl;
    getline(cin, line);   // Use getline() to read an entire line of text.

    // Break the line into words
    istringstream iss(line);   // create a string stream from the line
    while (iss >> word) {     // this will parse one word at a time.
	sentence.push_back(word);    // and add it to the back of the sentence vector
    }
    
    // Now print it out, one word per line to see if we did it right.
    for (unsigned i=0; i<sentence.size(); ++i) {
	cout << sentence[i] << '\n';
    }
#if 0
    for each word in the sentence{
newWord = translate(word);
store newWord in the translated sentence
}
for each word in the translated sentence{
print the word and a space
}

cout << "Do you want to do another sentence? ";
cin >> answer;
while (answer == "yes");

string translate(const string & word)
{
return word;

if stringLength 
#endif
    }
Oct 11, 2019 at 3:20pm
Thank you! How do I make the translator actually work and follow all the rules?? I know I'll need if statements but not completely sure how to go about that??
Oct 12, 2019 at 5:41pm
Write a translate() function that applies just the first rule. Once that's working you can move on to the other rules.

How much programming have you done until now?
Topic archived. No new replies allowed.