Substitution cipher: Difference between revisions

Content deleted Content added
CalmoSoft (talk | contribs)
Added Julia language
Line 726:
<pre>Encoded: "uTu]cu]b3Qu]<d]Id]...><K<,<Kd|]6KMbuTi
Decoded: Here we have to do is... Substitution Cipher.</pre>
 
=={{header|Julia}}==
{{works with|Julia|0.6}}
{{trans|Kotlin}}
 
'''Module''':
<lang julia>module SubstitutionCiphers
 
using Compat
 
const key = "]kYV}(!7P\$n5_0i R:?jOWtF/=-pe'AD&@r6%ZXs\"v*N[#wSl9zq2^+g;LoB`aGh{3.HIu4fbK)mU8|dMET><,Qc\\C1yxJ"
 
function encode(s::AbstractString)
buf = IOBuffer()
for c in s
print(buf, key[Int(c) - 31])
end
return String(take!(buf))
end
 
function decode(s::AbstractString)
buf = IOBuffer()
for c in s
print(buf, Char(findfirst(==(c), key) + 31))
end
return String(take!(buf))
end
 
end # module SubstitutionCiphers</lang>
 
'''Main''':
<lang julia>let s = "The quick brown fox jumps over the lazy dog, who barks VERY loudly!"
enc = SubstitutionCiphers.encode(s)
dec = SubstitutionCiphers.decode(enc)
println("Original: ", s, "\n -> Encoded: ", enc, "\n -> Decoded: ", dec)
end</lang>
 
{{out}}
<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
-> Decoded: The quick brown fox jumps over the lazy dog, who barks VERY loudly!</pre>
 
=={{header|Kotlin}}==