Created
March 20, 2016 10:48
-
-
Save mohammad19991/021f91e6e7124028f838 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// Original Source http://www.dotnetperls.com/rot13-swift | |
// Edited version | |
extension String { | |
func rot13() -> String { | |
// Empty character array. | |
var result = [Character]() | |
// Some ASCII constants. | |
// A = 65 | |
// M = 77 | |
// Z = 90 | |
// a = 97 | |
// m = 109 | |
// z = 122 | |
let upperA = 65 | |
let upperZ = 90 | |
let lowerA = 97 | |
let lowerZ = 122 | |
// Loop over utf8 values in string. | |
for u in self.utf8 { | |
let s = Int(u) | |
var resultCharacter = Character(UnicodeScalar(s)) | |
if s <= lowerZ && s >= lowerA { // Between a and z. | |
if s + 13 > lowerZ { | |
resultCharacter = Character(UnicodeScalar(s - 13)) | |
} else { | |
resultCharacter = Character(UnicodeScalar(s + 13)) | |
} | |
} else if s <= upperZ && s >= upperA { // Between A and Z. | |
if s + 13 > upperZ { | |
resultCharacter = Character(UnicodeScalar(s - 13)) | |
} else { | |
resultCharacter = Character(UnicodeScalar(s + 13)) | |
} | |
} | |
// Append to Character array. | |
result.append(resultCharacter) | |
} | |
// Return String. | |
return String(result) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment