A small program that takes a word (String) as input and translates it into Pig Latin
| 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