How to print characters that have odd frequencies of occurrence in a string with PHP

1 Answer

0 votes
function print_odd_frequencies_char($s) { 
        $letters = array();
        $letters = array_fill(0, 256, 0);
           
        for ($i = 0; $i < strlen($s); $i++) {
             if (ctype_alpha($s[$i])) {
                 $letters[ord($s[$i])]++;
             }
        }
            
        for ($i = 0; $i < 256; $i++) { 
            if ($letters[$i] != 0 && $letters[$i] % 2 != 0) {
                echo chr($i) . " " . $letters[$i] . "<br />";
            }
        } 
    } 

    
$s = "php programming pro oo"; 
           
print_odd_frequencies_char($s);
    
   
           
    
/*
run:
                
a 1
h 1
i 1
n 1
r 3
         
*/

 

 



answered Nov 30, 2019 by avibootz
...