Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $nums = '11225422';
- function lookandsay($num)
- { $output = '';
- $firstCharPos = 0;
- $endCharPos = 0;
- for ($i = 0; $i < strlen($num); $i++) {
- if( substr($num, $firstCharPos, 1) != substr($num, $i, 1) )
- {
- $digitsTotal = substr($num, $firstCharPos, $endCharPos);
- $output .= strlen($digitsTotal) . ':' . substr($num, $firstCharPos, 1). ' ';
- $firstCharPos = $i;
- $endCharPos = 0;
- }
- $endCharPos = $endCharPos + 1;
- }
- $digitsTotal = substr($num, $firstCharPos, strlen($num));
- $output .= strlen($digitsTotal) . ':' . substr($num, $firstCharPos, 1). ' ';
- return $output;
- }
- echo lookandsay($nums);
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement