Skip to content

Instantly share code, notes, and snippets.

@suresk
Created February 12, 2012 22:22
Show Gist options
  • Save suresk/1811157 to your computer and use it in GitHub Desktop.
Save suresk/1811157 to your computer and use it in GitHub Desktop.
Caeser Cipher Scala
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