September 2017 Update
This commit is contained in:
parent
bba7bfd280
commit
ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions
35
Task/Caesar-cipher/Kotlin/caesar-cipher.kotlin
Normal file
35
Task/Caesar-cipher/Kotlin/caesar-cipher.kotlin
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
// version 1.0.5-2
|
||||
|
||||
object Caesar {
|
||||
fun encrypt(s: String, key: Int): String {
|
||||
val offset = key % 26
|
||||
if (offset == 0) return s
|
||||
var d: Char
|
||||
val chars = CharArray(s.length)
|
||||
for ((index, c) in s.withIndex()) {
|
||||
if (c in 'A'..'Z') {
|
||||
d = c + offset
|
||||
if (d > 'Z') d -= 26
|
||||
}
|
||||
else if (c in 'a'..'z') {
|
||||
d = c + offset
|
||||
if (d > 'z') d -= 26
|
||||
}
|
||||
else
|
||||
d = c
|
||||
chars[index] = d
|
||||
}
|
||||
return chars.joinToString("")
|
||||
}
|
||||
|
||||
fun decrypt(s: String, key: Int): String {
|
||||
return encrypt(s, 26 - key)
|
||||
}
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
val encoded = Caesar.encrypt("Bright vixens jump; dozy fowl quack.", 8)
|
||||
println(encoded)
|
||||
val decoded = Caesar.decrypt(encoded, 8)
|
||||
println(decoded)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue