Simple parcourt des lettres d'une phrase
Sub CompteVoyelle()
Dim Phrase As String
Phrase = "Ceci est une phrase"
For Compteur = 1 To Len(Phrase)
MsgBox Mid(Phrase, Compteur, 1)
Next
End Sub
Comptage des e
Sub CompteVoyelleV2()
Dim Phrase As String
Dim NombreE As Integer
Phrase = "Ceci est une phrase"
For Compteur = 1 To Len(Phrase)
If Mid(Phrase, Compteur, 1) = "e" Then
NombreE = NombreE + 1
End If
Next
MsgBox NombreE
End Sub
Comptage des voyelles
Sub CompteVoyelleV3()
Dim Phrase As String
Dim NombreVoyelle As Integer
Phrase = "Ceci est une phrase"
NombreVoyelle = 0
For Compteur = 1 To Len(Phrase)
Select Case Mid(Phrase, Compteur, 1)
Case "a", "e", "i", "o", "u", "y"
NombreVoyelle = NombreVoyelle + 1
End Select
Next
MsgBox "Il y a " & NombreVoyelle & " voyelles dans " & Phrase
End Sub
Comptage plus précis de chaque voyelle, avec stockage dans un tableau
Sub CompteVoyelleV4()
Dim Phrase As String
Dim NombreVoyelle As Integer
Dim A, E, I, O, U, Y
Phrase = "Ceci est une phrase"
NombreVoyelle = 0
A = 0
E = 0
I = 0
O = 0
U = 0
Y = 0
For Compteur = 1 To Len(Phrase)
Select Case Mid(Phrase, Compteur, 1)
Case "a": a = A + 1
Case "e": E = E + 1
Case "i": I = I + 1
Case "o": O = O + 1
Case "u": U = U + 1
Case "y": Y = Y + 1
NombreVoyelle = NombreVoyelle + 1
End Select
Next
Selection.TypeText "Il y a " & A & " A dans " & Phrase
Selection.TypeParagraph
Selection.TypeText "Il y a " & E & " E dans " & Phrase
Selection.TypeParagraph
Selection.TypeText "Il y a " & I & " I dans " & Phrase
Selection.TypeParagraph
Selection.TypeText "Il y a " & O & " O dans " & Phrase
Selection.TypeParagraph
Selection.TypeText "Il y a " & U & " U dans " & Phrase
Selection.TypeParagraph
Selection.TypeText "Il y a " & Y & " Y dans " & Phrase
Selection.TypeParagraph
End Sub
- Compter toutes les lettres de l'alphabet
- N'afficher que les lettres présentes
- Montrer la lettre la plus courante
- Laisser le choix de la phrase à l'utilisateur
- Plus difficile : Compter le nombre de mots d'une phrase