-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcaesar_helper.rs
More file actions
35 lines (32 loc) · 831 Bytes
/
caesar_helper.rs
File metadata and controls
35 lines (32 loc) · 831 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
struct CaesarCipher {
shift: u32,
}
impl CaesarCipher {
fn new(shift: u32) -> Self {
Self { shift }
}
fn encode(&self, s: &str) -> String {
s.to_uppercase()
.chars()
.map(|e| {
if e >= 'A' && e <= 'Z' {
(((e as i32 - 65 + self.shift as i32) % 26) + 65) as u8 as char
} else {
e
}
})
.collect()
}
fn decode(&self, s: &str) -> String {
s.to_uppercase()
.chars()
.map(|e| {
if e >= 'A' && e <= 'Z' {
((((e as i32 - 65 - self.shift as i32) % 26 + 26) % 26) + 65) as u8 as char
} else {
e
}
})
.collect()
}
}