Anonymous user
Bacon cipher: Difference between revisions
→{{header|Kotlin}}: Updated example see https://github.com/dkandalov/rosettacode-kotlin for details
(→{{header|Kotlin}}: Updated example see https://github.com/dkandalov/rosettacode-kotlin for details) |
|||
Line 461:
=={{header|Kotlin}}==
The 'full' Bacon alphabet, which has separate letters for i, j, u and v, has been used in the following:
<lang scala>
private val codes = mapOf(
'a' to "AAAAA", 'b' to "AAAAB", 'c' to "AAABA", 'd' to "AAABB", 'e' to "AABAA",
)
fun encode(plainText: String, message: String): String {
Line 476:
for (c in pt)
if (c in 'a'..'z') sb.append(codes[c])
else
val et = sb.toString()
val mg = message.toLowerCase() // 'A's to be in lower case, 'B's in upper case
sb.setLength(0)
var count = 0
for (c in mg)
if (c in 'a'..'z') {
if (et[count] == 'A') sb.append(c)
else
count++
if (count == et.length) break
} else sb.append(c)
return sb.toString()
}
Line 496 ⟶ 495:
for (c in message)
when (c) {
}
val et = sb.toString()
sb.setLength(0)
for (i in 0 until et.length step 5) {
val quintet = et.substring(i, i + 5)
Line 512 ⟶ 511:
fun main(args: Array<String>) {
val plainText = "the quick brown fox jumps over the lazy dog"
val message
val cipherText
println("Cipher text ->\n\n$cipherText")
val decodedText = Bacon.decode(cipherText)
|