summaryrefslogtreecommitdiff
path: root/rust-book/piglatin/src/main.rs
blob: c5d9813af192eeacbb9a725012197119875d7427 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
use std::env;

fn print_usage() {
    eprintln!("error: usage: piglatin [word] [words]...");
}

fn main() {

    for mut word in env::args().skip(1) {
        // grab first letter
        let first = match word.get(0..1) {
            Some(s) => s,
            None => {
                print_usage();
                return;
            }
        };

        // check if first letter is vowel or consonant
        // if vowel, append -hay and print
        // if consonant, appent first letter and -ay and print
        match first {
            "a" | "e" | "i" | "o" | "u" | 
            "A" | "E" | "I" | "O" | "U" => {
                print!("{}-hay ", word);
            },
            _ => {
                let rest = match word.get(1..) {
                    Some(s) => s,
                    None => "",
                };
                print!("{}-{}ay ", rest, first);
            },
        }
    }

    println!();
}