Skip to content

Latest commit

 

History

History
19 lines (17 loc) · 576 Bytes

Reverse Words in a String III.md

File metadata and controls

19 lines (17 loc) · 576 Bytes

Given a string s, reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order.

Screen Shot 2021-11-15 at 20 13 46

var reverseWords = function(s) {
    let res = '';
    let word = '';
    for (let c of s) {
        if (c === ' ') {
            res += word + c;
            word = '';
        } else {
            word = c + word;
        }
    }
    return res + word;
};