Rotating letters in a string so that each letter is shifted to another letter by n places

前端 未结 4 1579
無奈伤痛
無奈伤痛 2021-01-05 20:07

I have been tasked with coming up with a way of encoding a string. Among other things, I need to shift each letter by a given number but the transformed letter must be a let

4条回答
  •  盖世英雄少女心
    2021-01-05 20:58

    Where your code goes wrong is when the shifted letter has a smaller character code than its original was, yet your code always adds a positive value to do the shift. You first need to detect when you are in this situation (for example, if the shifted letter is greater than z), and then make the appropriate fix (hint: consider the difference between the character code you want and the one you are getting now).

提交回复
热议问题