Caesar Cipher

  • + 0 comments

    Rust best solution

    If you’re looking for solutions to the 3-month preparation kit in either Python or Rust, you can find them below: my solutions

    fn caesar_cipher(s: &str, k: i32) -> String {
        //Time complexity: O(n)
        //Space complexity (ignoring input): O(n)
        let mut new_string = String::with_capacity(s.len());
        let k = k as u8;
        for letter in s.chars() {
            if letter.is_alphabetic() {
                let sum_a = if letter.is_uppercase() { b'A' } else { b'a' };
                new_string.push(((letter as u8 - sum_a + k) % (b'z' - b'a' + 1) + sum_a) as char);
            } else {
                new_string.push(letter);
            }
        }
        new_string
    }