Substitution cipher: Difference between revisions

Content added Content deleted
(Added Go)
(Scala contribution added.)
Line 1,311: Line 1,311:
</pre>
</pre>


=={{header|Scala}}==
<lang Scala>object SubstitutionCipher extends App {
private val key = "]kYV}(!7P$n5_0i R:?jOWtF/=-pe'AD&@r6%ZXs\"v*N" + "[#wSl9zq2^+g;LoB`aGh{3.HIu4fbK)mU8|dMET><,Qc\\C1yxJ"
private val text =
""""It was still dark, in the early morning hours of the twenty-second of December
| 1946, on the second floor of the house at Schilderskade 66 in our town,
| when the hero of this story, Frits van Egters, awoke."""".stripMargin

val enc = encode(text)
println("Encoded: " + enc)
println("Decoded: " + decode(enc))

private def encode(s: String) = {
val sb = new StringBuilder(s.length)
s.map {
case c if (' ' to '~').contains(c) => sb.append(key(c.toInt - 32))
case _ =>
}
sb.toString
}

private def decode(s: String) = {
val sb = new StringBuilder(s.length)
s.map {
case c if (' ' to '~').contains(c) =>
sb.append((key.indexOf(c.toInt) + 32).toChar)
case _ =>
}
sb.toString
}
}</lang>
{{Out}}See it running in your browser by [https://scalafiddle.io/sf/f9yNWk7/0 ScalaFiddle (JavaScript, non JVM)] or by [https://scastie.scala-lang.org/8CyCsxnnRZyn0JH0yegndA Scastie (JVM)].
=={{header|Sidef}}==
=={{header|Sidef}}==
{{trans|Julia}}
{{trans|Julia}}
Line 1,341: Line 1,373:
}</lang>
}</lang>
{{out}}
{{out}}
<pre>Original: The quick brown fox jumps over the lazy dog, who barks VERY loudly!
<pre>
Original: The quick brown fox jumps over the lazy dog, who barks VERY loudly!
-> Encoded: 2bu]E,KHm].Tdc|]4d\]),8M>]dQuT]<bu]U31C]Idf_]cbd].3Tm>]+ZzL]Ud,IUCk
-> Encoded: 2bu]E,KHm].Tdc|]4d\]),8M>]dQuT]<bu]U31C]Idf_]cbd].3Tm>]+ZzL]Ud,IUCk
-> Decoded: The quick brown fox jumps over the lazy dog, who barks VERY loudly!
-> Decoded: The quick brown fox jumps over the lazy dog, who barks VERY loudly!</pre>
</pre>


=={{header|Tcl}}==
=={{header|Tcl}}==