Write a PHP program to compare two given strings and return the number of the positions where they contain the same length 2 substring
<?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
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