Advertisement
Bruno

[PHP] Contar vogais e consoantes na palavra!

Jul 14th, 2012
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.88 KB | None | 0 0
  1. Criado por Bruno Pereira
  2. Acesse meu blog: www.projetosbrunopereira.com.br
  3.  
  4. /*
  5.     Para usar é simples, exemplo:
  6.     contarVogAndCons('Essa frase tem algumas vogais e consoantes!');
  7.    
  8.     Vai printar que tem 16 vogais e 27 consoantes!
  9.     É muito simples!
  10. */
  11. function contarVogAndCons($text) {
  12.     $textLower = strtolower($text);
  13.     $contText = strlen($text);
  14.     $contVogais;
  15.     for($bp; $bp != $contText; $bp++) {
  16.         switch($textLower[$bp]) {
  17.             case a: $contVogais++; break;
  18.             case e: $contVogais++; break;
  19.             case i: $contVogais++; break;
  20.             case o: $contVogais++; break;
  21.             case u: $contVogais++; break;
  22.         }
  23.     }
  24.     if($contVogais == 0)
  25.         echo "Não tem vogal!";
  26.     else
  27.         $cons = $contText-$contVogais;
  28.         echo "A frase <b>$text</b> tem <b>$contVogais</b> vogais!</br>A frase <b>$text</b> tem <b>$cons</b> consoantes!";
  29. }
  30. contarVogAndCons("Essa frase tem algumas vogais e consoantes!");
  31. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement