Created
December 15, 2016 10:59
-
-
Save aataraxiaa/8e280a83fd5898093a587861aea8f05e to your computer and use it in GitHub Desktop.
rot13 in Swift 3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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