Config Line Error Count

Config Line Error Count

Hard PHP PHP Error Handling 29 views
Explanation Complexity

Problem Statement

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.

Example

PORT=3000
=bad
NAME=app
nope
2

Constraints

Total text

Input / Output Format

Input Format
Multiple lines.
Output Format
One integer errors.
Constraints
Total text

Examples

Input:
PORT=3000 =bad NAME=app nope
Output:
2

Example Solution (Public)

PHP
<?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;
?>

Official Solution Code

<?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;
?>
Please login to submit solutions.
Editor
Output

                                        
Please login to submit solutions.