-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathCaeserCipher.swift
33 lines (30 loc) · 942 Bytes
/
CaeserCipher.swift
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
import Foundation;
_ = readLine()! // irrelavent
let S = readLine()!
let K = Int(readLine()!)!
// MARK: - Encryption Function
func encryptString(_ string: String, rotation: Int) -> String {
var encryptedString = String()
for char in string.unicodeScalars {
var code = Int(char.value)
// is lowercased letter
if (97...122).contains(char.value) {
code += rotation
while (code > 122) {
// reduce until in char range
code -= (122 - 96)
}
// is uppercased letter
} else if (65...90).contains(char.value) {
code += rotation
while (code > 90) {
// reduce until in char range
code -= (90 - 64)
}
}
encryptedString += String(Character(UnicodeScalar(code)!))
}
return encryptedString
}
// encrypt string
print(encryptString(S, rotation: K))