{"id":80455,"date":"2025-05-28T16:01:04","date_gmt":"2025-05-28T10:31:04","guid":{"rendered":"https:\/\/www.guvi.in\/blog\/?p=80455"},"modified":"2025-09-09T15:59:12","modified_gmt":"2025-09-09T10:29:12","slug":"how-to-take-string-input-and-print-in-javascript","status":"publish","type":"post","link":"https:\/\/www.guvi.in\/blog\/how-to-take-string-input-and-print-in-javascript\/","title":{"rendered":"JavaScript: How to Take String Input and Print Characters with Space (Node.js)\u00a0"},"content":{"rendered":"\n<p>When you&#8217;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.&nbsp;<\/p>\n\n\n\n<p>In this blog post, we\u2019ll learn how to <strong>read a string input<\/strong>, <strong>split it into characters<\/strong>, and <strong>print those characters separated by a space<\/strong> using JavaScript (Node.js).<\/p>\n\n\n\n<p>We&#8217;ll walk through:<\/p>\n\n\n\n<ul>\n<li>Understanding the input\/output format<br><\/li>\n\n\n\n<li>Using a common IDE-friendly template<br><\/li>\n\n\n\n<li>Applying the logic to a simple problem<br><\/li>\n\n\n\n<li>Final working solution with explanation<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Problem Statement<\/strong><\/h2>\n\n\n\n<p>Let\u2019s start with a simple problem.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Task:<\/strong><\/h3>\n\n\n\n<p>Write a program to get a string input and print each character separated by a space.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Input Description:<\/strong><\/h3>\n\n\n\n<p>A single line containing a string.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Output Description:<\/strong><\/h3>\n\n\n\n<p>Print all characters of the string separated by a space.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Sample:<\/strong><\/h3>\n\n\n\n<p><strong>Input:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>guvi<\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>g u v i<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Reusable Input Template for Online IDEs<\/strong><\/h2>\n\n\n\n<p>In most online coding platforms, input is taken through <strong>standard input (STDIN)<\/strong> and output through <strong>standard output (STDOUT)<\/strong>. Here\u2019s a basic template that helps us handle input in <a href=\"https:\/\/www.guvi.in\/blog\/guide-for-nodejs-as-backend\/\" target=\"_blank\" rel=\"noreferrer noopener\">Node.js<\/a> using the readline module:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>\/\/ Getting input via STDIN<br><strong>const<\/strong> readline = require(&#8220;readline&#8221;);<br><br><strong>const<\/strong> inp = readline.createInterface({<br>&nbsp; input: process.stdin,<br>&nbsp; output: process.stdout<br>});<br><br><strong>const<\/strong> userInput = [];<br><br>inp.on(&#8220;line&#8221;, (data) =&gt; {<br>&nbsp; userInput.push(data);<br>});<br><br>inp.on(&#8220;close&#8221;, () =&gt; {<br>&nbsp; \/\/ Your logic goes here<br>});<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>This template ensures we can work with inputs easily and plug in logic inside the .on(&#8220;close&#8221;) callback.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Applying the Logic to Our Problem<\/strong><\/h2>\n\n\n\n<p>Once we&#8217;ve captured the string input, we need to:<\/p>\n\n\n\n<ol>\n<li>Read the first line using userInput[0]<br><\/li>\n\n\n\n<li>Use .split(&#8221;) to break the string into characters<br><\/li>\n\n\n\n<li>Use .join(&#8216; &#8216;) to join them with spaces<br><\/li>\n\n\n\n<li>Print the result using console.log()<br><\/li>\n<\/ol>\n\n\n\n<p>Let\u2019s plug that into our template:<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Final Code<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>\/\/ Getting input via STDIN<br><strong>const<\/strong> readline = require(&#8220;readline&#8221;);<br><br><strong>const<\/strong> inp = readline.createInterface({<br>&nbsp; input: process.stdin,<br>&nbsp; output: process.stdout<br>});<br><br><strong>const<\/strong> userInput = [];<br><br>inp.on(&#8220;line&#8221;, (data) =&gt; {<br>&nbsp; userInput.push(data);<br>});<br><br>inp.on(&#8220;close&#8221;, () =&gt; {<br>&nbsp; <strong>const<\/strong> inputString = userInput[0]; \/\/ Read the first line<br>&nbsp; <strong>const<\/strong> output = inputString.split(&#8221;).join(&#8216; &#8216;); \/\/ Split into characters and join with space<br>&nbsp; console.log(output); \/\/ Print result<br>});<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Output for Sample Input<\/strong><\/h2>\n\n\n\n<p><strong>Input:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>guvi<\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>g u v i<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Recap<\/strong><\/h2>\n\n\n\n<ul>\n<li>We used Node.js and the readline module to take input from STDIN.<br><\/li>\n\n\n\n<li>The string was split into characters using .split(&#8221;).<br><\/li>\n\n\n\n<li>We joined them with spaces using .join(&#8216; &#8216;).<br><\/li>\n\n\n\n<li>This <a href=\"https:\/\/www.guvi.in\/blog\/objects-methods-and-classes-in-javascript\/\" target=\"_blank\" rel=\"noreferrer noopener\">method<\/a> works perfectly in most coding platforms and online judges.<\/li>\n<\/ul>\n\n\n\n<p><em>If you want to learn and <a href=\"https:\/\/www.guvi.in\/blog\/best-reasons-to-learn-javascript\/\" target=\"_blank\" rel=\"noreferrer noopener\">become proficient in JavaScript<\/a>, the <\/em><a href=\"https:\/\/www.guvi.in\/courses\/programming\/javascript-in-100-days\/?utm_source=blog&amp;utm_medium=hyperlink&amp;utm_campaign=How+to+Take+String+Input+and+Print+Characters+with+Space+in+JavaScript+%28Node.js%29+\" target=\"_blank\" rel=\"noreferrer noopener\"><em>JavaScript Course<\/em><\/a> in 100 days, by HCL GUVI,<em> is the perfect place to start. It offers a structured, hands-on learning path\u2014from basics like string manipulation to building full-fledged web apps\u2014all taught by industry experts.<\/em><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Concluding Thoughts\u2026<\/strong><\/h2>\n\n\n\n<p>Mastering <a href=\"https:\/\/en.wikipedia.org\/wiki\/String_(computer_science)\" target=\"_blank\" rel=\"noreferrer noopener\">string<\/a> input and output handling in <a href=\"https:\/\/www.guvi.in\/hub\/javascript\/\" target=\"_blank\" rel=\"noreferrer noopener\">JavaScript<\/a> (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.<\/p>\n\n\n\n<p>The techniques discussed are ideal for tackling basic string manipulation problems efficiently. Keep practicing, and these patterns will become second nature in no time!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When you&#8217;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.&nbsp; In this blog post, we\u2019ll learn how to read a string input, split it into characters, and print those characters separated by a space using JavaScript (Node.js). [&hellip;]<\/p>\n","protected":false},"author":41,"featured_media":82985,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[429,294,907],"tags":[],"views":"2758","authorinfo":{"name":"Arun Kumar","url":"https:\/\/www.guvi.in\/blog\/author\/arun-kumar\/"},"thumbnailURL":"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2025\/05\/Take-String-Input-and-Print-Characters-with-Space-in-JavaScript-Node.js--300x116.png","jetpack_featured_media_url":"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2025\/05\/Take-String-Input-and-Print-Characters-with-Space-in-JavaScript-Node.js-.png","_links":{"self":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts\/80455"}],"collection":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/users\/41"}],"replies":[{"embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/comments?post=80455"}],"version-history":[{"count":5,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts\/80455\/revisions"}],"predecessor-version":[{"id":86750,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts\/80455\/revisions\/86750"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/media\/82985"}],"wp:attachment":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/media?parent=80455"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/categories?post=80455"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/tags?post=80455"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}