Shifting letters on a specific interval

Disclosure: This is part of a homework assignment

In order to decode a message I need to shift letters by an amount "shift" that is established using code I've previously written.

For example: If shift = 4 then
a => w
d => z
m => i

etc.

My thought is that if I somehow do something like the following arithmetic I might be able to achieve what I want to: Assume that ch is a random letter

 
ch = static_cast<char>(ch - shift)


The problem is that around the edges (like values of a, b, c,) the above arithmetic will be converting my letters into characters like "^" etc.
You could try something like mod (%) along with the absolute value function (abs, found in <cmath>).

The easiest way to convert this back to a char is
1
2
3
char in = letter - 'a';
//create val using in and shift
char out = 'a' + val;
Topic archived. No new replies allowed.