Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $str = "aaabbbbddeecccf";
- function compressString($str)
- {
- $num = 1;
- $compressed = "";
- for($i = 0; $i <= strlen($str); $i++)
- {
- if($str[$i] == $str[$i + 1])
- {
- $num++;
- }
- else {
- if($num > 1)
- {
- $compressed .= $str[$i] . $num;
- }
- else
- {
- $compressed .= $str[$i];
- }
- $num = 1;
- }
- }
- return $compressed;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement