ReverseWords
JavaScript
easy
15 mins
Write a function reverseWords that takes a sentence string as input and returns a new string where each word is reversed, but the order of the words remains the same.
Words are separated by spaces. Preserve the original spacing.
Example Inputs & Outputs
reverseWords("Hello World") // → "olleH dlroW" reverseWords("JavaScript is fun") // → "tpircSavaJ si nuf" reverseWords(" Lead and Trail ") // → " daeL dna liarT " reverseWords("") // → "" reverseWords("OneWord") // → "droWenO"
Constraints & Edge Cases
- Input is always a string.
- Words are defined by space characters.
- Multiple spaces between words should be preserved.
- Leading/trailing spaces should also be preserved.
- Empty string input should return an empty string.
Solve Similar questions 🔥
Want to upskill? Explore our courses!
Namaste DSA
Master DSA from scratch with numerous problems, and expert guidance.
Namaste React
Wanna dive deep into React and become Frontend Expert? Learn with me now!
Namaste Frontend System Design
The most comprehensive and detailed course for frontend system design.
Namaste Node.js
Wanna dive deep into Node.js? Enroll into `Namaste Node.js` now!
