UVa 00492 – Pig-Latin
Because you consider PGP encryption for emails not secure enough, you decide to first convert the original message into Pig Latin, and then use PGP encryption to enhance security.
Therefore, you need to write a program that converts the original message into Pig Latin format.
Words should be converted to Pig Latin according to the following rules:
- Words that begin with a vowel (a, e, i, o, u, including their uppercase forms) should have the string "ay" appended after the word.
- For example, "apple" becomes "appleay" in Pig Latin.
- Words that begin with a consonant (any letter that is not a, e, i, o, u, including their uppercase forms) should have the first consonant removed and appended to the end of the word, followed by "ay."
- For example, "hello" becomes "ellohay" in Pig Latin.
- Do not change the case of any letters.
Sample Inputs/Outputs
Sample Input(s) | Sample Output(s) |
---|---|
The input is an original message consisting of multiple lines. | For the input message, please output the converted message. |
This is the input. | hisTay isay hetay inputay. |
Thought Process
Use getline to read a whole line and split the string into an array.
In string processing, you need to determine if the current string consists entirely of digits rather than alphabetic characters. If the string is composed of digits, no further processing is needed. However, if the string requires processing, you should check if it starts or ends with punctuation using the isalpha function. If punctuation marks are present, they should be removed before encryption and added back afterward.
Sample Code-ZeroJudge E627: Pig-Latin
#include <iostream>
#include <vector>
using namespace std;
bool isVowel(const char ch) {
if (ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U' || ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
return true;
}
return false;
}
string process (string s) {
if (s.empty()) {
s += 'a';
s += 'y';
return s;
}
if (!isVowel(s[0])) {
const char tmp = s[0];
s = s.substr(1, s.length());
s += tmp;
}
s += 'a';
s += 'y';
return s;
}
int main() {
cin.sync_with_stdio(0);
cin.tie(0);
string str;
while (getline(cin, str)) {
vector<string>v;
string tmp = "";
for (int i = 0; i<str.length(); i++) {
if (!isalpha(str[i]) && !tmp.empty()) {
if (str[i] != ' ') tmp += str[i];
v.push_back(tmp);
tmp = "";
continue;
}
if (str[i] != ' ') tmp += str[i];
}
if (!tmp.empty()) v.push_back(tmp);
vector<string>ans;
for (int i = 0; i<v.size(); i++) {
string s = v[i];
bool number = true;
for (int j = 0; j<s.length(); j++) {
if (!isdigit(s[j])) {
number = false;
break;
}
}
if (number) {
ans.push_back(s);
continue;
}
if (!isalpha(s[(int(s.length())-1)]) && !isalpha(s[0])) {
const char head = s[0], tail = s[(int(s.length())-1)];
string tmp = "";
for (int j = 1; j<s.length()-1; j++) tmp += s[j];
s = tmp;
s = process(s);
s += tail;
ans.push_back(head+s);
continue;
}
if (!isalpha(s[(int(s.length())-1)])) {
const char ch = s[(int(s.length())-1)];
s = s.substr(0, s.length()-1);
s = process(s);
s += ch;
ans.push_back(s);
continue;
}
ans.push_back(process(s));
}
for (int i = 0; i<ans.size(); i++) {
cout << ans[i];
if (i != ans.size()-1) cout << " ";
}
cout << "\n";
}
}
//ZeroJudge E627
//Dr. SeanXD