Example: Caesar Cipher

A Caesar cipher encrypts a string by replacing each letter with the letter k later in the alphabet, wrapping around at the end.

(define (caesar str k)
  (let* ((n (string-length str))
         (new (make-string n)))
    (dotimes (i n new)
      (string-set! new i
        (caesar-char (string-ref str i) k) ) ) ))

(define (caesar-char char k) (if (char-alphabetic? char) (let ((base (if (char-upper-case? char) (char->integer #\A) (char->integer #\a)))) (integer->char (+ base (modulo (+ k (- (char->integer char) base)) 26))) ) char))

; decryption (define (decaesar str k) (caesar str (- 26 k)))

Contents    Page-10    Prev    Next    Page+10    Index