Write a PHP program to count a substring of length 2 appears in a given string and also as the last 2 characters of the string
- برمجة بي اتش بي
- 2021-09-08
- mhanasmh00489829403
الأجوبة
<?php
function test($s)
{
$last_two_char = substr($s, strlen($s)-2, 2);
$ctr = 0;
for ($i = 0; $i < strlen($s)-2; $i++)
{
if (substr($s, $i, 2) == $last_two_char)
$ctr = $ctr +1;
}
return $ctr;
}
echo test("abcdsab")."\n";
echo test("abcdabab")."\n";
echo test("abcabdabab")."\n";
echo test("abcabd")."\n";
Sample Output:
1 2 3 0
أسئلة مشابهة
القوائم الدراسية التي ينتمي لها السؤال