PHP ZCE mock test, interview preparation, daily lessons under chalk talk

Thursday, January 12, 2012

ZCE Zend Certified Engineer Counter Problem


View the Complete Blog on ZCE

<?php

$string = "111221";

for($i = 0; $i < strlen($string); $i++) {
 
 $current = $string[$i];
 $count = 1;
 
 while(isset($string[$i + $count]) && ($string[$i + $count] == $current)) $count++;
 
 $newstring .= "$count{$current}";
 
 $i += $count-1;
}

print $newstring;
?>

Answer: 312211

No comments:

Post a Comment