How do i increment letters in c++?

后端 未结 8 1263
渐次进展
渐次进展 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:07

    Does letter++ work? All in all char is a numeric type, so it will increment the ascii code. But I believe it must be defined as char letter not an array. But beware of adding one to 'Z'. You will get '[' =P

    #include <iostream>
    
    int main () {
        char a = 'a';
        a++;
        std::cout << a;
    }
    

    This seems to work well ;)

    0 讨论(0)
  • 2021-01-05 06:09

    You can use 'a'+((letter - 'a'+n)%26); assuming after 'z' you need 'a' i.e. 'z'+1='a'

        #include <iostream>
    
        using namespace std;
    
        int main()
        {
           char letter='z';
           cout<<(char)('a' + ((letter - 'a' + 1) % 26));
    
           return 0;
        }
    

    See this https://stackoverflow.com/a/6171969/8511215

    0 讨论(0)
提交回复
热议问题