Can anyone help me to create a code to count how many words that are greater than 6? I am creating a readability test to calculate number of words etc. from three differente txt files. This is from my third and last private function. All help appreciated big time! :)

I have translated some of the words in the coding to make it easier to understand what it does...

Option Compare Database
Option Explicit
Private Const ordDeler As String = ",:;.!?"

`Private Function findLongWords(ByVal bok As String) As Double
Dim tegn As String, fil As String, linje As String
Dim fnr As Integer, i As Integer
Dim numberOfZeroSpace As Long, numberOfWords As Long, numberOfLongWords As Long

fnr = FreeFile
fil = CurrentProject.Path & "/" & bok & ".txt"
numberOfWords = 0
numberOfLongWords = 0
numberOfZeroSpace = 0
Open fil For Input As #fnr
Do While Not EOF(fnr)
    Line Input #fnr, linje
    For i = 1 To Len(linje)
        tegn = Mid(linje, i, 1)
        If InStr(1, ordDeler, tegn) > 6 Then
        numberOfLongWords = numberOfZeroSpace
        numberOfLongWords = numberOfLongWords + 1
     End If
   Next i
findLongWords = numberOfLongWords
End Function`

Recommended Answers

All 2 Replies

I think you should have a look at regular expressions. If you use the pattern \b[a-zA-Z']+\b it will return a Matches collection where each match is one word. You can loop over the matches collection and increment your coounter for eachc match that has a length > 6. The pattern breaks down as

\b              word boundary
[a-zA-Z']+      any string of lower/upper case letters or an apostrophe
\b              word boundary

While the pattern is not perfect you can start frm there and tailor it to your current word matching algorithm.

So o.k ifyou want to stick with vb6 then you could do the following;

Dim txtlen As Integer ' the length ofwords
Dim lencount As Integer ' the integer to hold the number of words longer then 6
lencount = 0 ' inialise to 0
Dim wordarray() As String' an array to hold all words
Dim rword As Variant ' the words we return fom the array
wordarray = Split(Trim$(RichTextbox1.Text), " ")' use the split function to from the string, in may case I use a rich textbox
For Each rword In wordarray
txtlen = Len(rword)
If txtlen > 6 Then
lencount = lencount + 1 ' found a word >6 increase the counter by 1
End If
Label1 = CStr(lencount) ' put the result into a label to display
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.