Read From Stdin Lines

Read From Stdin Lines

Easy NodeJS CLI Tools & Debugging 34 views
Explanation Complexity

Problem Statement

Read all stdin, split into lines, and print count.

Input Format

stdin: any text.

Output Format

Print integer.

Example

a\
b\
2

Constraints

Trim ending newline correctly.

Input / Output Format

Input Format
stdin: any text.
Output Format
Print integer.
Constraints
Trim ending newline correctly.

Examples

Input:
a\ b\
Output:
2

Example Solution (Public)

NodeJS
const fs = require('fs');
const text = fs.readFileSync(0, 'utf8');
if (!text) process.exit(0);
const lines = text.replace(/\
/g, '').split('\
');
if (lines.length && lines[lines.length - 1] === '') lines.pop();
console.log(lines.length);

Official Solution Code

const fs = require('fs');
const text = fs.readFileSync(0, 'utf8');
if (!text) process.exit(0);
const lines = text.replace(/\
/g, '').split('\
');
if (lines.length && lines[lines.length - 1] === '') lines.pop();
console.log(lines.length);
Please login to submit solutions.
Editor
Output

                                        
Please login to submit solutions.