Skip to content

Instantly share code, notes, and snippets.

@touchopia
Forked from jgallagher/ROT13.swift
Last active December 14, 2015 14:50
Show Gist options
  • Select an option

  • Save touchopia/c8bce477ed92f4bc2677 to your computer and use it in GitHub Desktop.

Select an option

Save touchopia/c8bce477ed92f4bc2677 to your computer and use it in GitHub Desktop.
//: ROT13 Swift 1.2 - by jgallagher
var rot13key = [Character:Character]()
let uppercase = Array("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
let lowercase = Array("abcdefghijklmnopqrstuvwxyz")
for var i = 0; i < 26; i++ {
rot13key[uppercase[i]] = uppercase[(i + 13) % 26]
rot13key[lowercase[i]] = lowercase[(i + 13) % 26]
}
func rot13(s: String) -> String {
return String(map(s, { rot13key[$0] ?? $0 }))
}
let encoded = rot13("Secret Message")
let decoded = rot13(encoded)
print("secret message encoded \(encoded)")
print("secret message decoded \(decoded)")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment