Q:

Write a PHP program to compare two given strings and return the number of the positions where they contain the same length 2 substring

0

Write a PHP program to compare two given strings and return the number of the positions where they contain the same length 2 substring

All Answers

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

<?php
function test($s1, $s2)
{
    $ctr = 0;
            for ($i = 0; $i < strlen($s1)-1; $i++)
            {
                $firstString = substr($s1, $i, 2);
                for ($j = 0; $j < strlen($s2)-1; $j++)
                {
                    $secondString = substr($s2, $j, 2);
                    if ($firstString == $secondString) 
                    $ctr++;
                }
            }
            return $ctr;
 }

echo (test("abcdefgh", "abijsklm"))."\n";
echo (test("abcde", "osuefrcd"))."\n";
echo (test("pqrstuvwx", "pqkdiewx"))."\n";

Sample Output:

1
1
2

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