Skip to content

Alphabetic Shift

Given a string, your task is to replace each of its characters by the next one in the English alphabet; i.e. replace a with b, replace b with c, etc (z would be replaced by a).

Example

For inputString = "crazy", the output should be

alphabetic_shift(inputString) = "dsbaz"

Solution

py
def alphabetic_shift(input_string):
    return ''.join(chr(ord(c) + 1) if c != 'z' else 'a' for c in input_string)


print(alphabetic_shift('z'))
js
function alphabetic_shift(inputString) {
  return inputString
    .split('')
    .map((char) => {
      if (char === 'z') {
        return 'a';
      }
      return String.fromCharCode(char.charCodeAt(0) + 1);
    })
    .join('');
}

console.log(alphabetic_shift('z'));

my thoughts are neither my employer's nor my wife's