Skip to content

Instantly share code, notes, and snippets.

@drakhavik
Last active October 27, 2020 18:40
Show Gist options
  • Save drakhavik/7e1297daff1ecf7dad194b73bf0d7b16 to your computer and use it in GitHub Desktop.
Save drakhavik/7e1297daff1ecf7dad194b73bf0d7b16 to your computer and use it in GitHub Desktop.
pig-latinize
fn is_vowel(c: char) -> bool {
match c {
'a' => true,
'A' => true,
'i' => true,
'I' => true,
'o' => true,
'O' => true,
'u' => true,
'U' => true,
_ => false,
}
}
fn to_pig_latin(input: &String) -> String {
let words: Vec<&str> = input.split(' ').collect();
let mut output = String::from("");
for word in words {
let mut word = String::from(word);
let first_letter = word.remove(0);
if is_vowel(first_letter) {
word = format!("{}{}-hay ", first_letter, word);
} else {
word = format!("{}-{}ay ", word, first_letter);
}
output.push_str(word.as_str());
}
output
}
fn main() {
// Small program to convert a string to pig-latin with the following rules:
// First consonant of each word is moved to the end of the word, and -ay is added
// first becomes irst-fay
// Words that start with a vowel have -hay addded to the end instead
// apple becomes apple-hay
// *This is an exercise from THE BOOK*
let to_convert = String::from("The quick brown fox jumps over the whatever the rest is");
println!("Original: {}", to_convert);
let converted = to_pig_latin(&to_convert);
println!("Converted: {}", converted);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment