Hello Tltbld,
There are a few ways to do this and I'll provide links to
implementations in Basic, C, and Python for reference. The two primary
ways are
- do arithmetic on each letter (add 3, if >Z, then subtract 26)
- use each letter to look up the replacement from a string or array
The following examples show both methods in a variety of programming
languages.
Basic
http://www.pbcrypto.com/view.php?algorithm=caesar
which converts each character to its numeric value, adds the key, and
converts back into a character. It does not strictly implement the
Ceasar algorithm since it does not limit the range to the alpha
characters but a simple change would fix that.
C
http://www.free2code.net/tutorials/c/cipher
a tutorial on ciphers with source code for ROT13 (a Ceasar cipher,
fixed at an offset of 13) and a monoaphabetic substitution cipher.
This example shows two ways to implement the Caesar cipher, by
arithmetic on the letters (similar to the Basic example) or by looking
up a value in a reference string.
Python
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/142813
this is perhaps the most straight forward version of the three. The
first function (named cipher) gets passed a string and key (# of
characters to shift) and generates the encoded character for all
letters in the range of 'a' to 'z'.
To find this information, a search of
ceasar cipher source code
was used.
--Maniac |