Q:

Write a PHP program to check if a given string contains between 2 and 4 'z' character

0

Write a PHP program to check if a given string contains between 2 and 4 'z' character.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

<?php
function test($s) 
{
   $ctr = 0;

            for ($i = 0; $i < strlen($s); $i++)
            {
                if (substr($s, $i, 1) == 'z')
                {
                    $ctr++;
                }
            }

            return $ctr > 1 && $ctr < 4;
}

var_dump(test("frizz"));
var_dump(test("zane"));
var_dump(test("Zazz"));
var_dump(test("false"));

Sample Output:

bool(true)
bool(false)
bool(true)
bool(false)

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now