
JavaScript: How to Take String Input and Print Characters with Space (Node.js)
May 28, 2025 2 Min Read 345 Views
(Last Updated)
When you’re learning to code especially JavaScript, one of the most fundamental skills is mastering how to handle inputs and outputs, especially in coding platforms or online IDEs.
In this blog post, we’ll learn how to read a string input, split it into characters, and print those characters separated by a space using JavaScript (Node.js).
We’ll walk through:
- Understanding the input/output format
- Using a common IDE-friendly template
- Applying the logic to a simple problem
- Final working solution with explanation
Table of contents
- Problem Statement
- Task:
- Input Description:
- Output Description:
- Sample:
- Reusable Input Template for Online IDEs
- Applying the Logic to Our Problem
- Final Code
- Output for Sample Input
- Recap
- Concluding Thoughts…
Problem Statement
Let’s start with a simple problem.
Task:
Write a program to get a string input and print each character separated by a space.
Input Description:
A single line containing a string.
Output Description:
Print all characters of the string separated by a space.
Sample:
Input:
guvi
Output:
g u v i
Reusable Input Template for Online IDEs
In most online coding platforms, input is taken through standard input (STDIN) and output through standard output (STDOUT). Here’s a basic template that helps us handle input in Node.js using the readline module:
// Getting input via STDIN const readline = require(“readline”); const inp = readline.createInterface({ input: process.stdin, output: process.stdout }); const userInput = []; inp.on(“line”, (data) => { userInput.push(data); }); inp.on(“close”, () => { // Your logic goes here }); |
This template ensures we can work with inputs easily and plug in logic inside the .on(“close”) callback.
Applying the Logic to Our Problem
Once we’ve captured the string input, we need to:
- Read the first line using userInput[0]
- Use .split(”) to break the string into characters
- Use .join(‘ ‘) to join them with spaces
- Print the result using console.log()
Let’s plug that into our template:
Final Code
// Getting input via STDIN const readline = require(“readline”); const inp = readline.createInterface({ input: process.stdin, output: process.stdout }); const userInput = []; inp.on(“line”, (data) => { userInput.push(data); }); inp.on(“close”, () => { const inputString = userInput[0]; // Read the first line const output = inputString.split(”).join(‘ ‘); // Split into characters and join with space console.log(output); // Print result }); |
Output for Sample Input
Input:
guvi
Output:
g u v i
Recap
- We used Node.js and the readline module to take input from STDIN.
- The string was split into characters using .split(”).
- We joined them with spaces using .join(‘ ‘).
- This method works perfectly in most coding platforms and online judges.
If you want to learn and become proficient in JavaScript, the JavaScript Course in 100 days, by GUVI, is the perfect place to start. It offers a structured, hands-on learning path—from basics like string manipulation to building full-fledged web apps—all taught by industry experts.
Concluding Thoughts…
Mastering string input and output handling in JavaScript (Node.js) is crucial for beginners, especially on online coding platforms. In this post, we explored how to read a string, split it into characters, and print them with spaces using a simple yet powerful approach.
The techniques discussed are ideal for tackling basic string manipulation problems efficiently. Keep practicing, and these patterns will become second nature in no time!
Did you enjoy this article?