PHP Online Compiler
Example: PHP program to find all the patterns of 0(1+)0 in the given string
C
C++
C#
Java
Python
PHP
main.php
STDIN
Run
<?php // PHP program to find all the patterns of 0(1+)0 in the given string // @This function will find the pattern function findPattern($str) { $last = $str[0]; $i = 1; $c = 0; // $c is the counter part while ($i < strlen($str)) { if ($str[$i] == '1' && $last == '0') { while ($i < strlen($str) && $str[$i] == '1') { $i++; } if ($i < strlen($str) && $str[$i] == '0') { $c++; } } $last = $i < strlen($str) ? $str[$i] : $last; $i++; } return $c; } // @It's the driver code $str = "1001001001"; echo "The Number of patterns found: " . findPattern($str) . "\n"; ?>
Output
Clear
ADVERTISEMENTS