Config Line Error Count
PHP
Hard
5 views
Problem Description
Read lines KEY=VALUE. Count how many lines are invalid (missing '=', empty key). Print error count.
Input Format
Multiple lines.
Output Format
One integer errors.
Sample Test Case
Input:
PORT=3000
=bad
NAME=app
nope
Official Solution
<?php
$inputText=stream_get_contents(STDIN);
$inputLines=preg_split('/\\R/', $inputText);
$err=0;
foreach($inputLines as $line){
$t=trim($line);
if($t==='') continue;
$pos=strpos($t,'=');
if($pos===false){ $err++; continue; }
$k=trim(substr($t,0,$pos));
if($k===''){ $err++; continue; }
}
echo $err;
?>
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!