Pig Latin Program

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;
}
}
}
Any help would be appreciated. Still not sure what the next step should be.
try this:
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#include <iostream>
#include <string>
using namespace 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.

Thank you for your help.


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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include <iostream>
#include <string>
using namespace 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;
}
}
Last edited on
gimme a minute and I will have the complete program...
Here:
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#include <vector>
#include <iostream>
#include <algorithm>
#include <string>
#include <iterator>
using namespace 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";
}
Last edited on
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
#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;
}
Last edited on
or you could do that...
Topic archived. No new replies allowed.