79331117

Date: 2025-01-05 16:46:58
Score: 1
Natty:
Report link

I was working on a problem where I needed to handle ASCII values within the alphabetical range. Specifically, if I add or subtract a value from a character, the result should wrap around the alphabet.

For example:

'z' + 2 should give 'b'

'a' - 2 should give 'y'

To achieve this, I used the following logic:

char c;
// Wrap around to the start of the alphabet
if (c > 'z')  
    c = c - 26;  

// Wrap around to the end of the alphabet<hr>
if (c < 'a')  
    c = c + 26;

This ensures the character stays within the range of 'a' to 'z'.**

Is this approach efficient, or are there other ways to achieve the same result? Any insights or suggestions are welcome!

Reasons:
  • Blacklisted phrase (0.5): I need
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Contains question mark (0.5):
  • Low reputation (1):
Posted by: hakuna matata