Converting lower & upper case ASCII characters

2019-09-09 17:20发布

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).

1条回答
▲ chillily
2楼-- · 2019-09-09 18:06

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).

查看更多
登录 后发表回答