header_logo
Post thumbnail
JAVASCRIPT

JavaScript: How to Take String Input and Print Characters with Space (Node.js) 

By Arun Kumar

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


  1. Problem Statement
    • Task:
    • Input Description:
    • Output Description:
    • Sample:
  2. Reusable Input Template for Online IDEs
  3. Applying the Logic to Our Problem
  4. Final Code
  5. Output for Sample Input
  6. Recap
  7. 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:

  1. Read the first line using userInput[0]
  2. Use .split(”) to break the string into characters
  3. Use .join(‘ ‘) to join them with spaces
  4. 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!

MDN

Success Stories

Did you enjoy this article?

Schedule 1:1 free counselling

Similar Articles

Loading...
Share logo Copy link
Power Packed Webinars
Free Webinar Icon
Power Packed Webinars
Subscribe now for FREE! 🔔
close
Webinar ad
Table of contents Table of contents
Table of contents Articles
Close button

  1. Problem Statement
    • Task:
    • Input Description:
    • Output Description:
    • Sample:
  2. Reusable Input Template for Online IDEs
  3. Applying the Logic to Our Problem
  4. Final Code
  5. Output for Sample Input
  6. Recap
  7. Concluding Thoughts…