diff --git a/challenges/caesar-cipher/solutions/rust/function/Cargo.lock b/challenges/caesar-cipher/solutions/rust/function/Cargo.lock new file mode 100644 index 0000000..c8ae10e --- /dev/null +++ b/challenges/caesar-cipher/solutions/rust/function/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "rust_application" +version = "1.0.0" diff --git a/challenges/caesar-cipher/solutions/rust/function/Cargo.toml b/challenges/caesar-cipher/solutions/rust/function/Cargo.toml new file mode 100644 index 0000000..a119ace --- /dev/null +++ b/challenges/caesar-cipher/solutions/rust/function/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "rust_application" +version = "1.0.0" +edition = "2021" + +[dependencies] diff --git a/challenges/caesar-cipher/solutions/rust/function/README.md b/challenges/caesar-cipher/solutions/rust/function/README.md new file mode 100644 index 0000000..a81c521 --- /dev/null +++ b/challenges/caesar-cipher/solutions/rust/function/README.md @@ -0,0 +1,3 @@ +# caesar-cipher/rust/function + +Created by [@Divlo](https://github.com/Divlo) on 4 January 2023. diff --git a/challenges/caesar-cipher/solutions/rust/function/src/main.rs b/challenges/caesar-cipher/solutions/rust/function/src/main.rs new file mode 100644 index 0000000..823d9a4 --- /dev/null +++ b/challenges/caesar-cipher/solutions/rust/function/src/main.rs @@ -0,0 +1,70 @@ +use std::io; + +pub struct ShiftedLetter { + pub origin: char, + pub shifted: char, +} + +pub fn shift_alphabet(shift: i32) -> Vec { + let mut shift = shift; + let mut result: Vec = Vec::new(); + let mut alphabet: Vec = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".chars().collect(); + let is_negative_shift = shift < 0; + if is_negative_shift { + alphabet.reverse(); + shift = shift.abs(); + } + for (index, letter) in alphabet.iter().enumerate() { + let mut shifted_index = (index as i32 + shift) as i32; + if shifted_index > (alphabet.len() - 1) as i32 { + shifted_index = (alphabet.len() as i32 - shifted_index).abs(); + } + let shifted_letter = alphabet[shifted_index as usize]; + result.push(ShiftedLetter { + origin: *letter, + shifted: shifted_letter, + }); + } + return result; +} + +pub fn caesar_cipher(string: &str, shift: i32) -> String { + if shift == 0 { + return string.to_string(); + } + let shifted_alphabet = shift_alphabet(shift); + let mut result = String::new(); + for letter in string.chars() { + if letter.is_whitespace() { + result.push(letter); + } else { + let mut found_letter = &shifted_alphabet[0]; + for shifted_letter in shifted_alphabet.iter() { + if shifted_letter.origin == letter { + found_letter = shifted_letter; + break; + } + } + result.push(found_letter.shifted); + } + } + return result; +} + +fn main() { + let mut string = String::new(); + io::stdin() + .read_line(&mut string) + .expect("Failed to read `stdin` line."); + let string = string.trim(); + + let mut shift = String::new(); + io::stdin() + .read_line(&mut shift) + .expect("Failed to read `stdin` line."); + let shift: i32 = shift + .trim() + .parse() + .expect("Failed to convert `width` as an `i32`."); + println!("{}", caesar_cipher(&string, shift)); +}