/* piglatin1.cpp -- Pig Latin Converter Rules for Pig Latin - Each word is converted individually according to the following rules: 1. If the word has no vowels (other than 'y', e.g. "my", "thy") append "yay" to it -- i.e., "myyay", "thyyay". 2. If the word begins with a vowel (e.g., "art", "else") append "yay" to the word (i.e., "artyay", "elseyay"). 3. If the word begins with a consonant (e.g., "song", "pig") divide the word at the first vowel, swapping the front and back halves and append "ay" to the word (i.e., "ongsay", "igpay") */ #include #include using namespace std; /* Returns the length of the longest head of a that has no characters in the string b. [Similar to strcspn */ int findFirst(const string& a, const string& b) { int first, where; first = where = a.length(); for (int k = 0; k < b.length(); ++k) { where = a.find(b[k]); if ((where != string::npos) && (first > where)) first = where; } return first; } // It returns the pig latin translation for the "given" word. string platin(const string& given) { // strcspn is a standard C function, see manual pages // int firstVowel = strcspn(given.c_str(), "aeiouAEIOU"); int firstVowel = findFirst(given, "aeiouAEIOU"); if ((firstVowel == 0) // starts with vowel || (firstVowel >= given.length()))// there are no vowels return given + "yay"; return given.substr(firstVowel, given.length() - firstVowel) + given.substr(0, firstVowel) + "ay"; } // Given a string s, it returns the string with all letters capitalized string capitalize(const string& s) { string code; for (int k = 0; k < s.length(); ++k) code += toupper(s[k]); return code; } void main (void) { string word; cout << "Enter a word to be converted: "; cin >> word; cout << "The Pig Latin for \"" << word << "\" is \"" << capitalize(platin(word)) << "\" " << endl; }