Write a function that takes a sentence as input and reverses the order of the words in that sentence. The reversed sentence should maintain the original spacing between words and should not alter the characters within each word.
Input: str = "Hey Aman is that correct way to reverse words of a string"
Output: "string a of words reverse to way correct that is Aman Hey"
function reverseWordsUsingSplitReverseJoin(str) {
return str.split(" ").reverse().join(" ");
}
const str1 = "Hey Aman is that correct way to reverse words of a string";
reverseWordsUsingSplitReverseJoin(str1); //output: string a of words reverse to way correct that is Aman Hey
function reverseWordsUsingReduce(str) {
return str.split(" ").reduce((reversed, word) => word + " " + reversed, "").trim();
}
const str1 = "Hey Aman is that correct way to reverse words of a string";
reverseWordsUsingReduce(str1); //output: string a of words reverse to way correct that is Aman Hey
function reverseWordsUsingForLoopAndPush(str) {
const words = [];
let word = "";
for (let i = 0; i < str.length; i++) {
if (str[i] === " ") {
words.push(word);
word = "";
} else {
word += str[i];
}
}
words.push(word);
return words.reverse().join(" ");
}
const str1 = "Hey Aman is that correct way to reverse words of a string";
reverseWordsUsingForLoopAndPush(str1); //output: string a of words reverse to way correct that is Aman Hey
function reverseWordsUsingForOfLoop(str) {
const words = [];
let word = "";
for (const char of str) {
if (char === " ") {
words.push(word);
word = "";
} else {
word += char;
}
}
words.push(word);
return words.reverse().join(" ");
}
const str1 = "Hey Aman is that correct way to reverse words of a string";
reverseWordsUsingForOfLoop(str1); //output: string a of words reverse to way correct that is Aman Hey
function reverseWordsUsingSplitAndMap(str) {
const words = str.split(" ");
return words.map((word, index, array) => array[array.length - 1 - index]).join(" ");
}
const str1 = "Hey Aman is that correct way to reverse words of a string";
reverseWordsUsingSplitAndMap(str1); //output: string a of words reverse to way correct that is Aman Hey
function reverseWordsUsingArrayFromAndForEach(str) {
const words = [];
Array.from(str).forEach((char) => {
if (char === " ") {
words.push(words.pop().trim());
words.push("");
} else {
if (words.length === 0) words.push("");
words[words.length - 1] += char;
}
});
words.push(words.pop().trim());
return words.reverse().join(" ");
}
const str1 = "Hey Aman is that correct way to reverse words of a string";
reverseWordsUsingArrayFromAndForEach(str1); //output: string a of words reverse to way correct that is Aman Hey
function reverseWordsUsingReplaceRegex(str) {
return str.replace(/(\S+)/g, (match, p1, offset, string) => {
const words = string.match(/\S+/g);
return words[words.length - 1 - words.indexOf(p1)];
});
}
const str1 = "Hey Aman is that correct way to reverse words of a string";
reverseWordsUsingReplaceRegex(str1); //output: string a of words reverse to way correct that is Aman Hey
function reverseWordsUsingSplitFilterReduce(str) {
return str.split(" ")
.filter(word => word.length > 0)
.reduce((reversed, word) => word + " " + reversed, "")
.trim();
}
const str1 = "Hey Aman is that correct way to reverse words of a string";
reverseWordsUsingSplitFilterReduce(str1); //output: string a of words reverse to way correct that is Aman Hey
function reverseWordsUsingStack(str) {
const stack = [];
let word = "";
for (let i = 0; i < str.length; i++) {
if (str[i] === " ") {
stack.push(word);
word = "";
} else {
word += str[i];
}
}
stack.push(word);
return stack.reverse().join(" ");
}
const str1 = "Hey Aman is that correct way to reverse words of a string";
reverseWordsUsingStack(str1); //output: string a of words reverse to way correct that is Aman Hey
function reverseWordsUsingSplitSortJoin(str) {
const words = str.split(" ");
return words.sort((a, b) => words.length - words.indexOf(a) - (words.length - words.indexOf(b))).join(" ");
}
const str1 = "Hey Aman is that correct way to reverse words of a string";
reverseWordsUsingSplitSortJoin(str1); //output: string a of words reverse to way correct that is Aman Hey