-
Notifications
You must be signed in to change notification settings - Fork 1
/
otan.rs
43 lines (35 loc) · 1.26 KB
/
otan.rs
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
39
40
41
42
43
use std::io::{self, BufRead};
fn otan() {
println!("Ecrivez le texte à transcrire en OTAN : ");
let stdin = io::stdin();
let mut handle = stdin.lock();
let mut otan_input = String::new();
handle.read_line(&mut otan_input).expect("Erreur lors de la lecture de l'entrée");
let otan_input = otan_input.trim().to_uppercase();
let mut translation = String::with_capacity(otan_input.len() * 6); // Pré-allouer suffisamment de capacité
for c in otan_input.chars() {
if let Some(otan_code) = lookup_otan(c) {
translation.push_str(otan_code);
translation.push(' ');
}
}
println!("\n{}\n", translation.trim());
}
fn lookup_otan(c: char) -> Option<&'static str> {
match c {
' ' => Some(" "),
'A'..='Z' => {
const OTAN_TABLE: [&str;26] = [
"Alpha", "Bravo", "Charlie", "Delta", "Echo", "Foxtrot", "Golf", "Hotel",
"India", "Juliet", "Kilo", "Lima", "Mike", "November", "Oscar", "Papa",
"Quebec", "Romeo", "Sierra", "Tango", "Uniform", "Victor", "Whiskey",
"Xray", "Yankee", "Zulu",
];
Some(OTAN_TABLE[(c as u8 - b'A') as usize])
}
_ => None,
}
}
fn main() {
otan();
}