Skip to content

Instantly share code, notes, and snippets.

@kosh04
Created October 27, 2009 19:44
Show Gist options
  • Save kosh04/219882 to your computer and use it in GitHub Desktop.
Save kosh04/219882 to your computer and use it in GitHub Desktop.
#!/usr/bin/clisp
;;; ROT13 (rotate by 13 places) algorithm implemented Common Lisp
;;; Example:
;; CL-USER> (rot13-string "HELLO") => "URYYB"
;; or
;; [shell]$ echo 'HELLO' | clisp rot13.lisp
;;; Source: $EMACS/lisp/rot13.el
;;; Code:
(defvar *rot13-translate-table*
(let ((str (make-string 127 :initial-element #\NUll)))
(dotimes (i 127)
(setf (elt str i) (code-char i)))
(dotimes (i 26)
(setf (elt str (+ i #1=#.(char-code #\a)))
(code-char (+ (mod (+ i 13) 26) #1#)))
(setf (elt str (+ i #2=#.(char-code #\A)))
(code-char (+ (mod (+ i 13) 26) #2#))))
str)
"String table for ROT13 translation.")
(defun rot13-char (char)
(handler-case
(elt *rot13-translate-table* (char-int char))
(error (e) char)))
(defun rot13-string (string)
(map 'string #'rot13-char string))
(defun main ()
(loop for line = (read-line nil nil)
while line
do (format t "~A~%" (rot13-string line))))
(main)
;;; EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment