visitor (0 QPoints)
  • FR
  • EN
  • NL
  • DE
  • ES
315 experts, 1193 registered users, 1659 questions already answered
European Experts Exchange, the very best site for high-quality IT solutions

New Improved Search!

 


05/10/2011 1h30 : Steve Jobs is dead, the father of Apple ][ is gone, we are all orphaned.

Languages :: PHP :: Knowledge Base : How to get run sequences from a string like "AAABCCCCDDAAAAEEE"


By: VGR France  Date: 25/03/2005 10:06:05  English  Points: 0 Status: Answered
Quality : Good
Dated 20/03/2003

<?php // //VGR20032003 EE // // initialisations $string="AAABCCCCDDAAAAEEE"; $elements=array(); // array[*] of String in high-level languages // loop $i=0; // counter of chars $j=0; // counter of elements $oldchar=$string[1]; while ($i<strlen($string)) { $curchar=$string[$i]; if ($curchar<>$oldchar) { // add found string before continuing $elements[$j].=$oldchar; $oldchar=$curchar; $j++; } else $elements[$j].=$curchar; // if we broke sequence $i++; } // while string not parsed completely $j++; // end of loop // display for ($i=0; $i<$j;$i++) echo "element $i = '{$elements[$i]}' "; ?>

By: VGR Date: 25/03/2005 10:07:45 English  Type : Comment
Results (add @ lines 15 and 18 to suppress NOTICEs if you run the E_ALL error reporting level)

element 0 = 'AAAA' element 1 = 'B' element 2 = 'CCCC' element 3 = 'DD' element 4 = 'AAAA' element 5 = 'EE'

By: VGR Date: 25/03/2005 10:09:07 English  Type : Comment
can be optimized (not computing strlen() in the for() loop declaration, etc) but that's a PHP-specific issue. "Normal" compiled languages do evaluate such a constant once at the start of the loop.

Do register to be able to answer

EContact
browser fav
page generated in 294.414040 milliseconds

Why Google AdSense ads ?

compteur
 Ranking-Hits PageRank for this page