[RUST/COMMON] 시저 암호(Caesar cipher) 만들기 (클로저 사용)
■ 클로저를 사용해 시저 암호(Caesar cipher)를 만드는 방법을 보여준다. ▶ 예제 코드 (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 |
fn encrypt(text : &str, shift : i16) -> String { let a_code = 'A' as i16; let converter = |character| (((character + a_code + shift + 26) % 26 + a_code) as u8) as char; let is_alpha = |character| 'A' <= character && character <= 'Z'; text.chars() .map(|character| if is_alpha(character) { converter(character as i16) } else { character }) .collect() } fn main() { let encrypted = encrypt("I LOVE YOU.", 3); let decrypted = encrypt(&encrypted , -3); println!("{} => {}", encrypted, decrypted); } /* L ORYH UXVW. => I LOVE RUST. */ |