Exercice 9.4 - Méthode 1: Compter nombre voyelles Terminé
Consigne
Ecrivez un algorithme qui demande une phrase à l’utilisateur et qui affiche à l’écran le nombre de voyelles contenues dans cette phrase. On pourra écrire deux solutions. La première déploie une condition composée bien fastidieuse. La deuxième, en utilisant la fonction Trouve, allège considérablement l'algorithme.
Pseudo code
iNbVoy, i des NUMERIQUES
stringChain une CHAINE DE CARACTERES
DEBUT
ECRIRE "Entre un chaîne de caractère"
Lire stringChain
iNbVoy = 0
POUR i 0 à len(stringChain)
SI (stringChain[i] === "a" || stringChain[i] === "e" || stringChain[i] === "i" || stringChain[i] === "o" || stringChain[i] === "u" || stringChain[i] === "y") ALORS
iNbVoy++
FIN SI
FIN POUR
ECRIRE "Nombre voyelle(s): " + iNbVoy
FIN
Conversion
function javascript() {
let output = document.querySelector('.response-displayer > .codeblock > pre#javascript');
output.classList.add('show');
let nStringChain = document.querySelector('input[name="stringChain"]').value;
let iNbVoy = 0;
for (let index = 0; index < nStringChain.length; index++) {
let sCurr = nStringChain[index];
if (sCurr === "a" || sCurr === "e" || sCurr === "i" || sCurr === "o" || sCurr === "u" || sCurr === "y")
{
iNbVoy = ( iNbVoy + 1 );
}
}
output.innerHTML = "[JS]: Il y a " + iNbVoy + " voyelle(s).";
}
function jquery() {
$('.response-displayer > .codeblock > pre').removeClass('show')
let $output = $('.response-displayer > .codeblock > pre#jquery').addClass('show')
let nStringChain = $('input[name="stringChain"]').val();
let iNbVoy = 0;
for (let index = 0; index < nStringChain.length; index++) {
let sCurr = nStringChain[index];
if (sCurr === "a" || sCurr === "e" || sCurr === "i" || sCurr === "o" || sCurr === "u" || sCurr === "y")
{
iNbVoy = ( iNbVoy + 1 );
}
}
$output.html("[jQuery]: Il y a " + iNbVoy + " voyelle(s).");
}
<?php
$stringChain = $_POST['stringChain'];
$iNbVoy = 0;
for ($i=0; $i < strlen($stringChain); $i++) {
if(
$stringChain[$i] === "a" ||
$stringChain[$i] === "e" ||
$stringChain[$i] === "i" ||
$stringChain[$i] === "o" ||
$stringChain[$i] === "u" ||
$stringChain[$i] === "y"
) {
$iNbVoy++;
}
}
echo "[PHP]: Il y a " . $iNbVoy . " voyelle(s).";
Choisissez un language:
Entrez les valeurs nécessaires:
Résultat du code
Le résultat s'affichera après l'exécution du code.