Write a PHP program to check if a given string contains between 2 and 4 'z' character.
<?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)
total answers (1)
start bookmarking useful questions and collections and save it into your own study-lists, login now to start creating your own collections.
Sample Output:
need an explanation for this answer? contact us directly to get an explanation for this answer