rotational-cipher: Updated the example solution to handle the negative shift key tests
-pub fn rotate(text: &str, shift_key: u8) -> String {
+pub fn rotate(text: &str, key: i8) -> String {
+ let key = if key < 0 { (26 + key) as u8 } else { key as u8 };
+
text.chars()
.map(|c| {
- let case = if c.is_uppercase() { 'A' } else { 'a' } as u8;
if c.is_alphabetic() {
- (((c as u8 - case + shift_key) % 26) + case) as char
+ let case = if c.is_uppercase() { 'A' } else { 'a' } as u8;
+
+ (((c as u8 - case + key) % 26) + case) as char
} else {
c
}
})
- .collect::<String>()
+ .collect()
}
