Advertisement
Guest User

Untitled

a guest
Oct 28th, 2016
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.38 KB | None | 0 0
  1. <?php
  2.  
  3. $str = "aaabbbbddeecccf";
  4.  
  5. function compressString($str)
  6. {
  7. $num = 1;
  8. $compressed = "";
  9.  
  10. for($i = 0; $i <= strlen($str); $i++)
  11. {
  12. if($str[$i] == $str[$i + 1])
  13. {
  14. $num++;
  15. }
  16. else {
  17. if($num > 1)
  18. {
  19. $compressed .= $str[$i] . $num;
  20. }
  21. else
  22. {
  23. $compressed .= $str[$i];
  24. }
  25.  
  26. $num = 1;
  27. }
  28. }
  29.  
  30. return $compressed;
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement