Guest User

Untitled

a guest
Apr 21st, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.97 KB | None | 0 0
  1. #!/usr/bin/clisp
  2.  
  3. ;;; ROT13 (rotate by 13 places) algorithm implemented Common Lisp
  4.  
  5. ;;; Example:
  6.  
  7. ;; CL-USER> (rot13-string "HELLO") => "URYYB"
  8. ;; or
  9. ;; [shell]$ echo 'HELLO' | clisp rot13.lisp
  10.  
  11. ;;; Source: $EMACS/lisp/rot13.el
  12.  
  13. ;;; Code:
  14.  
  15. (defvar *rot13-translate-table*
  16. (let ((str (make-string 127 :initial-element #\NUll)))
  17. (dotimes (i 127)
  18. (setf (elt str i) (code-char i)))
  19. (dotimes (i 26)
  20. (setf (elt str (+ i #1=#.(char-code #\a)))
  21. (code-char (+ (mod (+ i 13) 26) #1#)))
  22. (setf (elt str (+ i #2=#.(char-code #\A)))
  23. (code-char (+ (mod (+ i 13) 26) #2#))))
  24. str)
  25. "String table for ROT13 translation.")
  26.  
  27. (defun rot13-char (char)
  28. (handler-case
  29. (elt *rot13-translate-table* (char-int char))
  30. (error (e) char)))
  31.  
  32. (defun rot13-string (string)
  33. (map 'string #'rot13-char string))
  34.  
  35. (defun main ()
  36. (loop for line = (read-line nil nil)
  37. while line
  38. do (format t "~A~%" (rot13-string line))))
  39.  
  40. (main)
  41.  
  42. ;;; EOF
Add Comment
Please, Sign In to add comment