letreverseWords=(str)=>str.replace(/\S+/g,word=>[...word].reverse().join(""));console.log(reverseWords("The quick brown fox jumps over the lazy dog."));// ehT kciuq nworb xof spmuj revo eht yzal .godconsole.log(reverseWords("double spaced words"));// elbuod decaps sdrowconsole.log(reverseWords("🞀🞁🞂🞃"));// 🞃🞂🞁🞀
I avoided split("") as it breaks with non BMP characters. The spread operator works just as well for splitting strings into individual characters and won't break astral characters.
Notice how if I use split the output is messed up on my last test case.
letreverseWords=(str)=>str.replace(/\S+/g,word=>word.split("").reverse().join(""));console.log(reverseWords("The quick brown fox jumps over the lazy dog."));// ehT kciuq nworb xof spmuj revo eht yzal .godconsole.log(reverseWords("double spaced words"));// elbuod decaps sdrowconsole.log(reverseWords("🞀🞁🞂🞃"));// �🞂🞁🞀�
functionreverseWords($words){$pattern='/\S+/';returnpreg_replace_callback($pattern,function($word){returnstrrev($word[0]);},$words);}echoreverseWords('The quick brown fox jumps over the lazy dog.');echoreverseWords('double spaced words');
functionreverseWords(str){letwordbuf="";letoutbuf="";for(letcharofstr){// NOTE replace with `if (/\s/.test(char))`// if anything other than ASCII whitespace// char expectedif(char===""){outbuf+=(wordbuf+char);wordbuf="";}else{wordbuf=char+wordbuf;}}outbuf+=wordbuf;returnoutbuf;}
defreverseWords(words):r=""forwordinwords.split():r+="{} ".format(word[::-1])print(r)reverseWords("The quick brown fox jumps over the lazy dog.")reverseWords("double spaced words")
This version current works if the end of input pushes -1 on the stack (due to the interpreter I used), change 3 characters and it works for a carriage return
constreverseWords=(sentence)=>sentence.split("").map((word)=>[...word].reverse().join("")).join("");console.log(reverseWords("This is an example!"));console.log(reverseWords("double spaces"));
For Python, one line
Well done! It could also be
Looks great :)
I think this would fail with doubles spaces in a sentence...
That's true :( looks like the right implementation is
you don't need to use
(' ')to split by spaces :)Javascript
I avoided split("") as it breaks with non BMP characters. The spread operator works just as well for splitting strings into individual characters and won't break astral characters.
Notice how if I use split the output is messed up on my last test case.
For php
Here are the PHP codes about using
foreachandforloops to complete:JavaScript (imperative & linear):
More python:
Or more simply:
Befunge-93
This version current works if the end of input pushes -1 on the stack (due to the interpreter I used), change 3 characters and it works for a carriage return
JavaScript (ES6) (golf)
ES6:
For Java
Another Python:
Try it online!
Perl:
Try it online!
For Elixir
Java Script
Simple python example:

Python one-liner:
JavaScript :
Without libraries:
reverseWords = unwords . map reverse . words
// JavaScript
function reverseWord(word){
return word.split("").reverse().join('');
}
// JavaScript
const reverseWord = word => word.split("").reverse().join('');
JS:
str.split(" ").map(w => w.split("").reverse().join("")).join(" ")
Very elegant.