Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
40
Task/Caesar-cipher/V-(Vlang)/caesar-cipher.v
Normal file
40
Task/Caesar-cipher/V-(Vlang)/caesar-cipher.v
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
import rand
|
||||
|
||||
const
|
||||
(
|
||||
lo_abc = 'abcdefghijklmnopqrstuvwxyz'
|
||||
up_abc = 'ABCDEFGHIJKLMNIPQRSTUVWXYZ'
|
||||
)
|
||||
|
||||
fn main() {
|
||||
key := rand.int_in_range(2, 25) or {13}
|
||||
encrypted := caesar_encrypt('The five boxing wizards jump quickly', key)
|
||||
println(encrypted)
|
||||
println(caesar_decrypt(encrypted, key))
|
||||
}
|
||||
|
||||
fn caesar_encrypt(str string, key int) string {
|
||||
offset := key % 26
|
||||
if offset == 0 {return str}
|
||||
mut nchr := u8(0)
|
||||
mut chr_arr := []u8{}
|
||||
for chr in str {
|
||||
if chr.ascii_str() in up_abc.split('') {
|
||||
nchr = chr + u8(offset)
|
||||
if nchr > u8(90) {nchr -= 26}
|
||||
}
|
||||
else if chr.ascii_str() in lo_abc.split('') {
|
||||
nchr = chr + u8(offset)
|
||||
if nchr > u8(122) {nchr -= 26}
|
||||
}
|
||||
else {
|
||||
nchr = chr
|
||||
}
|
||||
chr_arr << nchr
|
||||
}
|
||||
return chr_arr.bytestr()
|
||||
}
|
||||
|
||||
fn caesar_decrypt(str string, key int) string {
|
||||
return caesar_encrypt(str, 26 - key)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue