Substitution cipher: Difference between revisions

m
no edit summary
(Added Algol 68)
mNo edit summary
 
(4 intermediate revisions by the same user not shown)
Line 1,156:
Hello world
</pre>
 
 
=={{header|Emacs Lisp}}==
<syntaxhighlight lang="lisp">
(setq alphabet "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
(setq code "odrmqbjnwzvueghlacyfpktisxODRMQBJNWZVUEGHLACYFPKTISX")
 
(defun encode (text)
"Encode TEXT with simple substitution code.
Each letter is replaced with the a random letter.
A specific letter in the original will always be replaced
by the same coded letter."
(let* ((case-fold-search nil)
(text-length (length text))
(encoded-text "")
(current-letter "")
(coded-letter "")
(alphabet-position))
(dotimes (i text-length)
(setq current-letter (substring text i (+ 1 i))) ; find the next letter in TEXT
(if (not (string-match-p "[a-zA-Z]" current-letter)) ; IF it's not a letter
(setq coded-letter current-letter) ; pass the non-letter without change
(setq alphabet-position (string-match current-letter alphabet)) ; ELSE find where the letter is in ALPHABET
(setq coded-letter (substring code alphabet-position (+ 1 alphabet-position)))) ; AND pass the coded letter
(setq encoded-text (concat encoded-text coded-letter))) ; IN ANY CASE, add new letter to ENCODED-TEXT
encoded-text))
 
(defun decode (text)
"Decode TEXT encoded with simple substitution code.
Each coded letter is replaced with the corresponding
uncoded letter."
(let* ((case-fold-search nil)
(text-length (length text))
(uncoded-text "")
(current-letter "")
(uncoded-letter "")
(code-position))
(dotimes (i text-length)
(setq current-letter (substring text i (+ 1 i))) ; find the next letter in TEXT
(if (not (string-match-p "[a-zA-Z]" current-letter)) ; IF it's not a letter
(setq uncoded-letter current-letter) ; pass the non-letter without change
(setq code-position (string-match current-letter code)) ; ELSE find where the letter is in CODE
(setq uncoded-letter (substring alphabet code-position (+ 1 code-position)))) ; AND pass the uncoded letter
(setq uncoded-text (concat uncoded-text uncoded-letter))) ; IN ANY CASE, add new letter to UNCODED-TEXT
uncoded-text))
 
</syntaxhighlight>
 
{{out}}
(encode "Call me Ishmael. Some years ago—never mind how long precisely—having
little or no money in my purse, and nothing particular to interest me
on shore, I thought I would sail about a little and see the watery part
of the world. ")
<pre>
"Rouu eq Wyneoqu. Yheq sqocy ojh—gqkqc ewgm nht uhgj lcqrwyqus—nokwgj
uwffuq hc gh ehgqs wg es lpcyq, ogm ghfnwgj locfwrpuoc fh wgfqcqyf eq
hg ynhcq, W fnhpjnf W thpum yowu odhpf o uwffuq ogm yqq fnq tofqcs locf
hb fnq thcum. "
</pre>
(decode "Rouu eq Wyneoqu. Yheq sqocy ojh—gqkqc ewgm nht uhgj lcqrwyqus—nokwgj
uwffuq hc gh ehgqs wg es lpcyq, ogm ghfnwgj locfwrpuoc fh wgfqcqyf eq
hg ynhcq, W fnhpjnf W thpum yowu odhpf o uwffuq ogm yqq fnq tofqcs locf
hb fnq thcum. ")
<pre>
"Call me Ishmael. Some years ago—never mind how long precisely—having
little or no money in my purse, and nothing particular to interest me
on shore, I thought I would sail about a little and see the watery part
of the world. "
</pre>
 
 
=={{header|Factor}}==
26

edits