Write a PHP program to check if the first appearance of "a" in a given string is immediately followed by another "a"
<?php function test($s) { $counter = 0; for ($i = 0; $i < strlen($s)-1; $i++) { if (substr($s, $i, 1) == 'a') $counter++; if ((substr($s, $i, 2) == 'aa') && $counter < 2) return true; } return false; } var_dump(test("caabb")); var_dump(test("babaaba")); var_dump(test("aaaaa"));
Sample Output:
bool(true) bool(false) bool(true)
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