Reversing a string while maintaining the position of spaces in JavaScript



Problem

We are required to write a JavaScript function that takes in a string that might contain some spaces.

Our function should reverse the words present in the string internally without interchange the characters of two separate words or the spaces.

Advertisement
00:00
00:00

Example

Following is the code −

 Live Demo

const str = 'this is normal string';
const reverseWordsWithin = (str = '') => {
   let res = "";
   for (let i = str.length - 1; i >= 0; i--){
      if(str[i] != " "){
         res += str[i];
      };
      if(str[res.length] == " "){
         res += str[res.length];
      };
   };
   return res;
};
console.log(reverseWordsWithin(str));

Output

gnir ts lamron sisiht
Updated on: 2021-04-20T09:25:01+05:30

776 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements