Workspace Reverse Words In A String
0d
Back to questionsDSA Visualizers: 388 Coding Questions

Reverse Words in a String visualizer

Learn Reverse Words in a String with a step-by-step Strings visualizer, JavaScript source code and a worked example. Practice fundamentals and parsing.

INTERACTIVE EXECUTION STUDIO
LEARN BY DOING

Predict. Execute. Understand.

Follow the source code, inspect live state changes, and predict the next operation before revealing it.

1 / 3Execution step
Reference example · JavaScript
Inspect reference input
"  hello world  "

Algorithm source

JavaScript reference implementation · highlighted trace statement
const solve = (a,s)=>{
let words=a.trim().split(/\s+/),o=[];
for(let i=words.length-1;
i>=0;
i--){
o.push(words[i]);
s("Read word from right",{
Words:words,Output:o
},[i])
}
return o.join(" ")
};

Execution canvas

Active Changed
STEP 1

Read word from right

Inspect the highlighted code and the state below. Click individual cells or nodes to see their values.

Words2 items
Output1 items
Keyboard: Space to play · Left / Right to stepExplore next problem

Worked example and algorithm walkthrough

Topic: Strings · Fundamentals and parsing

Follow the computed execution on the reference input. Advance one step at a time, inspect the changing values and compare your prediction with the final output.

Example input

"  hello world  "

Execution steps

  1. Read word from right

Computed output

"world hello"
JavaScript reference implementation
const solve = (a,s)=>{
  let words=a.trim().split(/\s+/),o=[];
  for(let i=words.length-1;
  i>=0;
  i--){
    o.push(words[i]);
    s("Read word from right",{
      Words:words,Output:o
    },[i])
  }
  return o.join(" ")
};

Continue learning

Practice related DSA questions in the same topic.