How do i increment letters in c++?

后端 未结 8 1264
渐次进展
渐次进展 2021-01-05 05:30

I\'m creating a Caesar Cipher in c++ and i can\'t figure out how to increment a letter.

I need to increment the letter by 1 each time and return the next letter in t

8条回答
  •  鱼传尺愫
    2021-01-05 06:05

    It works as-is, but because the addition promotes the expression to int you want to cast it back to char again so that your IOStream renders it as a character rather than a number:

    int main() {
       char letter[] = "a";
       cout << static_cast(letter[0] + 1);
    }
    

    Output: b

    Also add wrap-around logic (so that when letter[0] is z, you set to a rather than incrementing), and consider case.

提交回复
热议问题