Enforce lower/uppercase consistency; expand readme

This commit is contained in:
Smaug123
2016-01-04 09:31:35 +00:00
parent 80e9d6e8c8
commit 37e40640a0
7 changed files with 71 additions and 35 deletions

View File

@@ -1,14 +1,15 @@
"""
Encrypts the given plaintext according to the Caesar cipher.
The key is given as an integer, being the offset of each character;
so encrypt_caesar("abc", 1) == "bcd".
so encrypt_caesar("abc", 1) == "BCD".
Converts the input to lowercase.
Converts the input to uppercase.
"""
function encrypt_caesar(plaintext, key::Integer)
# plaintext: string; key: integer offset, so k=1 sends "a" to "b"
key = ((key-1) % 26) + 1
encrypt_monoalphabetic(plaintext, join([collect(Char(97+key):'z'); collect('a':Char(97+key-1))]))
keystr = join([collect(Char(97+key):'z'); collect('a':Char(97+key-1))])
encrypt_monoalphabetic(plaintext, keystr)
end
"""
@@ -19,7 +20,7 @@ so decrypt_caesar("abcd", 1) == "zabc".
Converts the input to lowercase.
"""
function decrypt_caesar(ciphertext, key::Integer)
# ciphertext: string; key: integer offset, so k=1 decrypts "B" as "A"
# ciphertext: string; key: integer offset, so k=1 decrypts "B" as "a"
key = ((key-1) % 26) + 1
encrypt_caesar(ciphertext, 26-key)
lowercase(encrypt_caesar(ciphertext, 26-key))
end