Advertisement
Nicklaj

Esercizio

Mar 7th, 2020
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.70 KB | None | 0 0
  1. <?php
  2.     $value = str_split(strtolower($_GET["input"]), 1);
  3.     $i = 0;
  4.     $j = 0;
  5.     $fr = [];
  6.     foreach($value as $char){
  7.         if(($char >= 'a' && $char <= 'z')) {
  8.             $i += 1;
  9.         } elseif($char >= '0' || $char <= '9'){
  10.             $j += 1;
  11.         }
  12.        
  13.         if(array_key_exists($char, $fr)) {
  14.             $v = explode('.', $fr[$char])[1] + 1;
  15.             $fr[$char] = "$char.$v";
  16.         } else {
  17.             $fr[$char] = "$char.1";
  18.         }
  19.     }
  20.    
  21.     echo "Sono presenti $i vocali e consonanti\n";
  22.     echo "Sono presenti $j caratteri numerici\n";
  23.     foreach($fr as $elem)
  24.     {
  25.         $v = explode(".", $elem)[1] / count($fr) * 100;
  26.         $l = explode(".", $elem)[0];
  27.         echo "Il carattere $l ha frequenza $v%\n";
  28.     }
  29. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement