Skip to content

Instantly share code, notes, and snippets.

@aataraxiaa
Created December 15, 2016 10:59
Show Gist options
  • Save aataraxiaa/8e280a83fd5898093a587861aea8f05e to your computer and use it in GitHub Desktop.
Save aataraxiaa/8e280a83fd5898093a587861aea8f05e to your computer and use it in GitHub Desktop.
rot13 in Swift 3
// rot 13
var rot13Mapped = [Character:Character]()
let upperCase = Array("ABCDEFGHIJKLMNOPQRSTUVWXYZ".characters)
let lowerCase = Array("abcdefghijklmnopqrstuvwxyz".characters)
for (index, _) in upperCase.enumerated() {
rot13Mapped[upperCase[index]] = upperCase[(index + 13) % 26]
rot13Mapped[lowerCase[index]] = lowerCase[(index + 13) % 26]
}
func rot13(forString string: String) -> String {
return String(string.characters.map { rot13Mapped[$0] ?? $0 })
}
print(rot13(forString: "Pete"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment