27 lines
559 B
Plaintext
27 lines
559 B
Plaintext
func Char.Encrypt(code) {
|
|
if !this.IsLetter() {
|
|
return this
|
|
}
|
|
var offset = (this.IsUpper() ? 'A' : 'a').Order()
|
|
return Char((this.Order() + code - offset) % 26 + offset)
|
|
}
|
|
|
|
func String.Encrypt(code) {
|
|
var xs = []
|
|
for c in this {
|
|
xs.Add(c.Encrypt(code))
|
|
}
|
|
return String.Concat(values: xs)
|
|
}
|
|
|
|
func String.Decrypt(code) {
|
|
this.Encrypt(26 - code);
|
|
}
|
|
|
|
var str = "Pack my box with five dozen liquor jugs."
|
|
print(str)
|
|
str = str.Encrypt(5)
|
|
print("Encrypted: \(str)")
|
|
str = str.Decrypt(5)
|
|
print("Decrypted: \(str)")
|