How do i increment letters in c++?

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

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

        #include 
    
        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

提交回复
热议问题