Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- fn main() {
- let input = "MEET AT DAWN";
- let key = "ALLY";
- println!("{}", cipher(input, key))
- }
- fn cipher(input: &str, key: &str) -> String {
- let mut key = key.chars().cycle();
- input
- .chars()
- .map(|c| if c.is_whitespace() { c } else { key.next().unwrap() })
- .collect()
- }
- fn cipher_fold(input: &str, key: &str) -> String {
- input.chars().fold((String::new(), 0), |(mut acc, i), c| {
- if c.is_whitespace() {
- acc.push(c);
- (acc, i)
- } else {
- let k = key.chars().nth(i).unwrap();
- acc.push(k);
- (acc, (i + 1) % key.len())
- }
- }).0
- }
- #[test]
- fn test_cipher() {
- let input = "MEET AT DAWN";
- let key = "ALLY";
- let expected = "ALLY AL LYAL";
- assert_eq!(cipher(input, key), expected);
- assert_eq!(cipher_fold(input, key), expected);
- }
Add Comment
Please, Sign In to add comment