Q:

Write a PHP program to count the string "aa" in a given string and assume "aaa" contains two "aa"

0

Write a PHP program to count the string "aa" in a given string and assume "aaa" contains two "aa".

All Answers

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

<?php
function test($s)
{
   $ctr_aa = 0;
   for ($i = 0; $i < (strlen($s) - 1); $i++)
            {
                if (substr($s, $i, 2) == "aa")
                {
                    $ctr_aa++;
                }
            }
            return $ctr_aa;
 }


echo test("bbaaccaag")."\n";
echo test("jjkiaaasew")."\n";
echo test("JSaaakoiaa")."\n";

Sample Output:

2
2
3

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