-
Notifications
You must be signed in to change notification settings - Fork 0
/
HMC_0703.cpp
112 lines (94 loc) · 1.76 KB
/
HMC_0703.cpp
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
/*Chapter 7: Programming Exercise #3
*Made by Heather Connor
*/
#include<iostream>
#include<string>
#include <string.h>
using namespace std;
bool isVowel(char ch);
string rotate(string pigLatin);
string pigLatinStr(string pigLatin);
int main()
{
string engIn; //Declare user input variable
cout << "Welcome to the Pig Latin Translator!" << endl
<< "This program translates English into Pig Latin." << endl;
cout << "English: ";
getline(cin, engIn);
cout << endl;
cout << "Pig Latin: " << pigLatinStr(engIn) << endl;
return 0;
}
bool isVowel(char ch)
{
switch (ch)
{
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
case 'Y':
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
case 'y':
return true;
default:
return false;
}
}
bool isPunct(char ch)
{
switch (ch)
{
case ',':
case ';':
case':':
case '&':
case '.':
case '?':
case '!':
return true;
default:
return false;
}
}
string rotate(string pigLatin)
{
string::size_type len = pigLatin.length();
string rStr;
rStr = pigLatin.substr(1, len - 1) + pigLatin[0];
return rStr;
}
string pigLatinStr(string pigLatin)
{
string::size_type len;
bool foundVowel;
bool foundPunct;
string::size_type counter;
if (isVowel(pigLatin[0]))
pigLatin = pigLatin + "-way";
else
{
pigLatin = pigLatin + '-';
pigLatin = rotate(pigLatin);
len = pigLatin.length();
foundVowel = false;
for (counter = 1; counter < len - 1; counter++)
if (isVowel(pigLatin[0]))
{
foundVowel = true;
break;
}
else
pigLatin = rotate(pigLatin);
if (!foundVowel)
pigLatin = pigLatin.substr(1, len) + "-way";
else
pigLatin = pigLatin + "ay";
}
return pigLatin;
}