Skip to content

Instantly share code, notes, and snippets.

@zachwhaley
Last active January 22, 2020 18:54
Show Gist options
  • Save zachwhaley/9442902 to your computer and use it in GitHub Desktop.
Save zachwhaley/9442902 to your computer and use it in GitHub Desktop.
A C++ program to convert a sentence to Pig Latin
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
// is_vowel returns true if the given
// character is a vowel; false otherwise.
bool is_vowel(char c)
{
c = tolower(c);
return c == 'a'
or c == 'e'
or c == 'i'
or c == 'o'
or c == 'u'
or c == 'y'
;
}
// to_piglatin translates a string to piglatin.
//
// For words that begin with consonant sounds,
// the initial consonant or consonant cluster
// is moved to the end of the word, and "ay"
// is added.
// For words that begin with vowel sounds or
// silent letter, you just add "way".
string to_piglatin(string& s)
{
auto it = find_if(begin(s), end(s), is_vowel);
if (it == begin(s)) {
s += "way";
}
else if (it != end(s)) {
rotate(begin(s), it, end(s));
s += "ay";
}
return s;
}
int main(int argc, char* argv[])
{
string s;
for (int i = 1; i < argc; i++) {
s = argv[i];
cout << to_piglatin(s) << " ";
}
cout << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment