Mdjon26
Mdjon26

Reputation: 2255

Converting lower & upper case ASCII characters

I'm making a program that converts ascii characters 'a' through 'z' and 'A' through 'Z'. (only letters). for example, a+1 = b

a+2 = c

b+1 = c

A+1 = B

So the only thing I'm not sure how to do is mapping around. How can I make it so that when checklower/checkupper is true, to basically map around to the lower case letter (example, of z+2 = b).

Upvotes: 0

Views: 775

Answers (1)

Carl Norum
Carl Norum

Reputation: 224864

The simplest way is probably to use the % modulus operator:

int letter_add = ((input.at(i) - 'a' + cmd_int) % 26) + 'a';

You'll need a symmetrical line for capital letters (or just make the 'a' a variable too).

Upvotes: 3

Related Questions