DevX Home Today's Headlines   Articles Archive   Tip Bank   Forums

# Thread: Count Vowels

1. Registered User
Join Date
Dec 2007
Posts
10

## Count Vowels

Ok, everyone get ready to laugh.

I need to be able to count the number of vowels in a string, and display the count in a textbox. How would I do that?

(Please don't ask why )

2. Originally Posted by RichardGR
(Please don't ask why )
Ok, I won't (but, I really would like to know. )

Anyway, try something like this
Code:
```Private Function VowelCount(ByVal InputString As String) As Long

Dim Vowel(11) As String
Dim NumberOfVowels As Long
Dim VowelFlag As Long
Dim LengthOfString As Long
Dim i As Long

'arrary of possible vowels
Vowel(0) = "a"
Vowel(1) = "i"
Vowel(2) = "o"
Vowel(3) = "u"
Vowel(4) = "e"
Vowel(5) = "y"
Vowel(6) = "A"
Vowel(7) = "I"
Vowel(8) = "O"
Vowel(9) = "U"
Vowel(10) = "E"
Vowel(11) = "Y"

LengthOfString = Len(InputString)

For VowelFlag = 1 To LengthOfString

For i = 0 To 11
If Mid(InputString, VowelFlag, 1) = Vowel(i) Then
NumberOfVowels = NumberOfVowels + 1
End If
Next
Next

VowelCount = NumberOfVowels
MsgBox VowelCount
End Function```

3. Senior Member
Join Date
Nov 2003
Location
Portland, OR
Posts
8,387
Here's another:
Code:
```Dim I As Integer
Dim nVowels As Integer
Dim Vowels As String
Dim InputString As String

Vowels = "aeiou"
InputString = LCase(InputString)

For I = 1 To Len(InputString)
If InStr(Vowels, Mid(InputString, I, 1)) Then
nVowels = nVowels + 1
End If
Next

MsgBox nVowels```

4. Senior Member
Join Date
Nov 2003
Location
Portland, OR
Posts
8,387
One more:
Code:
```Dim exp As RegExp
Dim Matches As MatchCollection
Dim nVowels As Integer
Dim InputString As String

Set exp = New RegExp
With exp
.Pattern = "[aeiou]"
.Global = True
.IgnoreCase = True
Set Matches = .Execute(InputString)
End With

nVowels = Matches.Count```

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

 FAQ Latest Articles Java .NET XML Database Enterprise
 Questions? Contact us. C++ Web Development Wireless Latest Tips Open Source

×
We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.