Reverse Every Word in a String

easy

By - Aman Pareek

Last Updated - 28/08/2024

Problem Statement

Write a function that takes a sentence as input and reverses each word within the sentence individually while maintaining the original word order and spacing.

Example 1

Input: str = "Hey Aman is that correct way to reverse words of a string"

Output: "yeH namA si taht tcerroc yaw ot esrever sdrow fo a gnirts"

Solution 1: Using split, map, and join

function reverseEachWordUsingSplitMapJoin(sentence) {
  return sentence.split(" ")
                 .map(word => word.split("").reverse().join(""))
                 .join(" ");
} 

const str1 = "Hey Aman is that correct way to reverse words of a string";
reverseEachWordUsingSplitMapJoin(str1);  //output: yeH namA si taht tcerroc yaw ot esrever sdrow fo a gnirts 

Solution 2: Using a for loop

function reverseEachWordUsingForLoop(sentence) {
  let words = sentence.split(" ");
  for (let i = 0; i < words.length; i++) {
    words[i] = words[i].split("").reverse().join("");
  }
  return words.join(" ");
} 

const str1 = "Hey Aman is that correct way to reverse words of a string";
reverseEachWordUsingForLoop(str1);  //output: yeH namA si taht tcerroc yaw ot esrever sdrow fo a gnirts 

Solution 3: Using for...of loop

function reverseEachWordUsingForOfLoop(sentence) {
  const words = sentence.split(" ");
  const reversedWords = [];
  for (const word of words) {
    reversedWords.push(word.split("").reverse().join(""));
  }
  return reversedWords.join(" ");
} 

const str1 = "Hey Aman is that correct way to reverse words of a string";
reverseEachWordUsingForOfLoop(str1);  //output: yeH namA si taht tcerroc yaw ot esrever sdrow fo a gnirts 

Solution 4: Using reduce

function reverseEachWordUsingReduce(sentence) {
  return sentence.split(" ")
                 .reduce((acc, word) => acc + (acc ? " " : "") + word.split("").reverse().join(""), "");
} 

const str1 = "Hey Aman is that correct way to reverse words of a string";
reverseEachWordUsingReduce(str1);  //output: yeH namA si taht tcerroc yaw ot esrever sdrow fo a gnirts 

Solution 5: Using Array.from and forEach

function reverseEachWordUsingArrayFromForEach(sentence) {
  return sentence
    .split(" ")
    .map((word) => Array.from(word).reverse().join(""))
    .join(" ");
} 

const str1 = "Hey Aman is that correct way to reverse words of a string";
reverseEachWordUsingArrayFromForEach(str1);  //output: yeH namA si taht tcerroc yaw ot esrever sdrow fo a gnirts 

Solution 6: Using map with split and reverse

function reverseEachWordUsingMapSplitReverse(sentence) {
  return sentence
    .split(" ")
    .map((word) => [...word].reverse().join(""))
    .join(" ");
} 

const str1 = "Hey Aman is that correct way to reverse words of a string";
reverseEachWordUsingMapSplitReverse(str1);  //output: yeH namA si taht tcerroc yaw ot esrever sdrow fo a gnirts 

Solution 7: Using split, reverse, and reduce

function reverseEachWordUsingSplitReverseReduce(sentence) {
  return sentence.split(" ")
                 .map(word => word.split("").reverse().join(""))
                 .reduce((acc, word) => acc + (acc ? " " : "") + word, "");
} 

const str1 = "Hey Aman is that correct way to reverse words of a string";
reverseEachWordUsingSplitReverseReduce(str1);  //output: yeH namA si taht tcerroc yaw ot esrever sdrow fo a gnirts 

Solution 8: Using split and manual string reversal

function reverseEachWordUsingSplitManual(sentence) {
  return sentence.split(" ")
                 .map(word => {
                   let reversedWord = "";
                   for (let i = word.length - 1; i >= 0; i--) {
                     reversedWord += word[i];
                   }
                   return reversedWord;
                 })
                 .join(" ");
} 

const str1 = "Hey Aman is that correct way to reverse words of a string";
reverseEachWordUsingSplitManual(str1);  //output: yeH namA si taht tcerroc yaw ot esrever sdrow fo a gnirts 

Solution 9: Using String.prototype.replace

function reverseEachWordUsingReplace(sentence) {
  return sentence.replace(/\S+/g, word => word.split("").reverse().join(""));
} 

const str1 = "Hey Aman is that correct way to reverse words of a string";
reverseEachWordUsingReplace(str1);  //output: yeH namA si taht tcerroc yaw ot esrever sdrow fo a gnirts 

Solution 10: Using split, for loop, and join

function reverseEachWordUsingSplitForJoin(sentence) {
  const words = sentence.split(" ");
  const reversedWords = [];
  for (let i = 0; i < words.length; i++) {
    let reversedWord = "";
    for (let j = words[i].length - 1; j >= 0; j--) {
      reversedWord += words[i][j];
    }
    reversedWords.push(reversedWord);
  }
  return reversedWords.join(" ");
} 

const str1 = "Hey Aman is that correct way to reverse words of a string";
reverseEachWordUsingSplitForJoin(str1);  //output: yeH namA si taht tcerroc yaw ot esrever sdrow fo a gnirts 

More Problems Solutions

Reverse words in a string