Created
November 17, 2018 16:42
-
-
Save goderich/16587c73dc907f717832650eabb5843d to your computer and use it in GitHub Desktop.
A small program that takes a word (String) as input and translates it into Pig Latin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::io; | |
fn main() { | |
let mut input = String::new(); | |
io::stdin().read_line(&mut input) | |
.expect("Failed to read line"); | |
println!("{}", piglatinize(&input)); | |
} | |
fn piglatinize(s: &str) -> String { | |
let mut answer = s.to_string(); | |
let _ = match answer.pop() { | |
None => panic!("Empty string"), | |
Some(c) => c, | |
}; | |
let len = answer.len(); | |
let initial_letter = first_char(&s); | |
let consonant_initial = is_consonant_initial(initial_letter); | |
if consonant_initial { | |
answer = answer[1..len] .to_string(); | |
answer.push(initial_letter); | |
answer.push_str("-ay"); | |
} else { | |
answer.push_str("-hay"); | |
} | |
format!("{}", answer) | |
} | |
fn first_char(s: &str) -> char { | |
let mut chars = s.chars(); | |
match chars.next() { | |
None => panic!("Empty string"), | |
Some(c) => c, | |
} | |
} | |
fn is_consonant_initial(c: char) -> bool { | |
for vowel in ['a', 'i', 'u', 'e', 'o'].iter() { | |
if c == *vowel { | |
return false; | |
} | |
} | |
true | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment