racket

How to encode a message?


I'm trying to write a function which produces an encoded version of a string where all the letters are swapped with the letter at the same relative position at the other end of the alphabet.

For example:

an A would become a Z and a Z would become an A; a B would become a Y and a Y would become a B. All numeric characters stay the same and all non-alpha numeric characters are removed.


Solution

  • The key is the function char->integer.

    Here is a little expression to get you started:

    (for/list ([c "hello world"])
       (define i (- (char->integer c) (char->integer #\a)))
       (integer->char (- (char->integer #\z) i)))
    

    You need to figure out how to handle characters outside a-z correctly.