How to shift each letter in the given string N places down in the alphabet in JavaScript?


In the given problem statement our aim is to shift every letter in the given string N places down in the alphabet with the help of Javascript functionalities. So we will use some basic Javascript methods to complete the given task.

Understanding the Problem

The problem at hand is to shift the character of the given string up to the N places down in the alphabets with the help of Javascript. The purpose of this problem is we need to take a string as input and we have to update the given string by shifting every letter N positions down in the alphabet. For example, if the letter A is shifted by 2 places down so it will become C and if Y is shifted to 2 places down then it will become A. So at the end we have to show the result after shifting all the characters of the given string.

Logic for the given Problem

To solve this problem we will create a function which will accept two parameters as input: first is the string str and second is the number of places to shift each letter N. In this function we will define the alphabet as a string which will contain all the character in lowercase letters. And then we will use regular expressions to match all the lowercase and uppercase letters in the string.

Now for every matched letter we will convert it in the lowercase and we will find its current index in the alphabet string. After that we will calculate the new index with the addition of N to the current index and check that it wraps around the alphabet with the help of modulo operator %. After that we will retire the shifted character from the alphabet string at the new index. And also we will convert if there are capital letters present in the original string. Finally show the updated string with the shifted letters.

Algorithm

Step 1: As we have to shift the given input string to n places down in the alphabet so for doing this task we will create a function and give it a name as shiftString(). This function accepts two parameters: first is string str and second is number N. N represents that we have to shift each character to N places down.

Step 2: Inside the function we will declare a string of all the alphabets and give it a name as the alphabet. This string will be used to check the index of each letter present in the input string and also to shift each letter N places down.

Step 3: After declaring the alphabet string we will use regular expressions to match all lowercase and uppercase letters present in the string. If there is any uppercase letter present in the string then convert it in the lowercase.

Step 4: Then check the index of every letter present in the string.

Step 5: As we have to shift each letter to N places down, in this step we will shift down each letter to N places and wrap it around the alphabet.

Step 6: Now the shifted characters will be stored in the shiftedChar variable.

Step 7: At the second last step we will also check if there was any uppercase letter present so at the place of that letter we will again convert it to the uppercase letter for the shiftedChar string.

Step 8: At the end we will return the shiftedStr as a result.

Example

//Function to shirt the string n places down
function shiftString(str, N) {
   const alphabet = 'abcdefghijklmnopqrstuvwxyz';
   const shiftedStr = str.replace(/[a-z]/gi, (match) => {
      const lowercase = match.toLowerCase();
      const currentIndex = alphabet.indexOf(lowercase);
      const newIndex = (currentIndex + N) % 26; // Wrapping around the alphabet
      const shiftedChar = alphabet[newIndex];
      return (lowercase === match) ? shiftedChar : shiftedChar.toUpperCase();
   });
   return shiftedStr;
}

const inputString = 'Hello, Tutorials Point!';
const shiftedString = shiftString(inputString, 3);
console.log(shiftedString);

Output

Khoor, Wxwruldov Srlqw!

Complexity

The time complexity for the given problem of shifting each letter in the given string N places down in the alphabet is O(n), in which n is the size of the input string. As the code traverses through the letter in the given string and performs operations to shift each letter down in the alphabet. The code utilizes memory to store the alphabet string which is a fixed length of 26 characters. So the space complexity is O(1) as it does not depend on the size of the input string.

Conclusion

The code we have implemented effectively solve the given problem of shifting each characters N places down in the given string. The code has linear time complexity which ensures that the code will handle the input strings of any length. And code also maintains the constant amount of space complexity.

Updated on: 14-Aug-2023

942 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements