Q:

Write a PHP program to check if the first appearance of "a" in a given string is immediately followed by another "a"

0

Write a PHP program to check if the first appearance of "a" in a given string is immediately followed by another "a"

All Answers

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

<?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)

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