> For the complete documentation index, see [llms.txt](https://algorithm.prettylog.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://algorithm.prettylog.com/algorithm-problems/algoexpert/medium/reverse-words-in-string.md).

# Reverse Words in String

![](/files/AMKgU1SFk1UjLgK6121w)

* n, n

```tsx
function reverseWordsInString(str) {

  const arr = [];
  let word = [];
  for (let i = 0; i < str.length; i++) {
    const curr = str[i];
    if (curr !== ' ') {
      word.push(curr);
      continue;
    }
    
    arr.unshift(...word);
    arr.unshift(curr)
    word = [];
  }

  if (word.length > 0) 
    arr.unshift(...word);

  return arr.join('');
}

// Do not edit the line below.
exports.reverseWordsInString = reverseWordsInString;
```
