How do I increment a variable to the next or previous letter in the alphabet?

前端 未结 5 629
别跟我提以往
别跟我提以往 2021-02-04 03:48

I have a capital letter defined in a variable string, and I want to output the next and previous letters in the alphabet. For example, if the variable was equal to \'C\'

5条回答
  •  别跟我提以往
    2021-02-04 04:23

    Well if you mean the 'ABC' then they split into two sequences a-z and A-Z, the simplest way I think would be to use a char variable and to increment the index by one.

    char letter='c';
    letter++;  // (letter=='d')
    

    same goes for decrement:

    char letter='c';
    letter--; // (letter=='b')
    

    thing is that the representation of the letters a-z are 97-122 and A-Z are 65-90, so if the case of the letter is important you need to pay attention to it.

提交回复
热议问题