String Encryption Problem
We are to create an encryption function.
It will implement a substitution cypher.
For simplicity, it assumes that the string it is encoding
is all lowercase letters or spaces.
It takes a source string, a destination string and a key.
It also assumes the destination string is long enough.
Inputs
The main() gets a string as input from the user and a key.
It passes these to the encryption function
and prints the encrypted and clear strings.
Outputs
The initial string is printed and so is the encrypted version.
Notes
A substitution cypher is a very simple code.
It is often invented by young children.
The key is a small number that is used as an offset into the
alphabet.
Each characater in the cleartext string is replaced by a letter
offset by the key.
If the encoded character is not in the range of the alphabet,
we wrap around to the beginning of the alphabet.
Whitespace is unchanged.
A couple of examples:
Key is 3
a --> d
b --> e
c --> f
...
w --> z
x --> a
y --> b
z --> c
Answer
Here is one solution to this problem.