Created
February 12, 2012 22:22
-
-
Save suresk/1811157 to your computer and use it in GitHub Desktop.
Caeser Cipher Scala
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
def printChar(i: Int) = print(i.toChar) | |
def decipherChar(c: Char, n: Int) = c match { | |
case x if x < 97 || x > 122 => printChar(x) | |
case x if x - n < 97 => printChar(122 - (97 - (x -n)) + 1) | |
case _ => printChar(c - n) | |
} | |
def decipher(str: String, n: Int) = str.toLowerCase.toArray.foreach(decipherChar(_, n)) | |
def bruteDecipher(str: String) = (1 to 25).foreach { i: Int => | |
println("Deciphering using shift of: " + i) | |
decipher(str, i) | |
println("\n") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment