String Function - CountWords

This short routine shows how to code a routine to return the number of words in a string (including strings containing CR/LF codes).
'
'Description :
'
'"Countwords" function can be used to determine number of
'words in a multiline string. The function breaks the source string
'into lines and passes each line to the function "countwordsinline"
'to get number of words in each line.
' 
' 
public Function countwordsinline(byval sourceline as string) _
       as Long
    Dim nextspaceindex as Long
    sourceline = sourceline & " "
'Every word has space at its end
    nextspaceindex = InStr(sourceline, " ")
    While nextspaceindex <> 0
        If nextspaceindex <> 1 then countwordsinline = _
           countwordsinline + 1
        sourceline = mid$(sourceline, nextspaceindex + 1)
        nextspaceindex = InStr(sourceline, " ")
    Wend
End Function
'
public Function countwords(byval source as string) as Long
    Dim endindex as Long
    source = source & Chr$(13)
    endindex = InStr(source, Chr$(13))
    While (endindex <> 0)
        countwords = countwords + countwordsinline(mid$(source, _
                                                   1, endindex - 1))
        source = mid$(source, endindex + 2)
'VB vbCrLf contains 2 Characters
        endindex = InStr(source, Chr$(13))
    Wend
End Function
'


Comments

  • This is much simpler...

    Posted by Legacy on 01/09/2004 12:00am

    Originally posted by: Dave W

    Regular expressions have a word boundary character. Just apply it to the string and divide the length of the resulting collection by 2 (since each word has a beginning and ending boundary)...
    
    

    Function wordcount(instring)
    Dim regex, matches
    Set regex = New RegExp
    regex.Pattern = "\b"
    regex.Global = True
    Set matches = regex.Execute(instring)
    wordcount = matches.count /2
    Set regex = nothing
    Set matches = nothing
    End Function

    Reply
  • Count words

    Posted by Legacy on 08/01/2003 12:00am

    Originally posted by: G.Subramani

    Option Explicit
    
    

    Function CountFields(LineDataIn As String, _
    Delimiter As String) As Integer
    Dim NewPos As Integer
    Dim MaxPos As Integer
    Dim FieldCounter As Integer

    If LineDataIn = "" Or Delimiter = "" Then
    CountFields = 0
    Exit Function
    End If

    MaxPos = Len(LineDataIn)
    NewPos = 1
    FieldCounter = 0

    While (NewPos < MaxPos) And (NewPos <> 0)
    NewPos = InStr(NewPos, LineDataIn, _
    Delimiter, vbTextCompare)
    If NewPos <> 0 Then
    FieldCounter = FieldCounter + 1
    NewPos = NewPos + 1
    End If
    Wend
    CountFields = FieldCounter
    End Function


    Sub CmdTest_Click()
    'This is just a test routine and isn't required....
    'Just here to show how the code can be used.

    Dim NumberOfFields As Integer
    Dim Delimiter As String
    Dim LineDataIn As String

    Delimiter = InputBox$("Type a Field Delimiter", "DEMO:Delimited Field Counter", ",")
    LineDataIn = InputBox$("Enter a Delimted String to have its fields counted", "DEMO:Delimited Field Counter")

    NumberOfFields = CountFields(LineDataIn, Delimiter)
    MsgBox "There are: " + Str$(NumberOfFields) + " fields in the string (" + LineDataIn + ")", 64, "DEMO:Delimited Field Counter"
    End Sub

    Reply
  • Count Words

    Posted by Legacy on 11/15/2001 12:00am

    Originally posted by: Chris Lucas

    I have a much faster word count routine, it also uses less lines of code.  Hey less to maintain and fewer CPU cycles, how can you lose?
    
    

    At the top of a module in the project place the following API function:

    Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" ( _
    Destination As Any, _
    Source As Any, _
    ByVal Length As Long)

    Now the function itself:

    Public Function WordCount(Text As String) As Long
    Dim dest() As Byte
    Dim i As Long

    If LenB(Text) Then
    ReDim dest(LenB(Text))
    CopyMemory dest(0), ByVal StrPtr(Text), LenB(Text) -1
    For i = 0 To UBound(dest) Step 2
    If dest(i) > 32 Then
    Do Until dest(i) < 33
    i = i + 2
    Loop
    WordCount = WordCount + 1
    End If
    Next i
    Erase dest
    Else
    WordCount = 0
    End If
    End Function

    Thats it. Speed is highly dependant on the string itself, but on my box with a string of 50,000 words all seperated by one space, this function performed 87 times faster.
    One important note: WordCount deals with extra spaces, and all manner of carriage returns flawlessly. Please direct any comments or questions to cdl1051@earthlink.net.

    --Chris

    Reply
  • countwords

    Posted by Legacy on 10/06/2001 12:00am

    Originally posted by: zul

    could you give more example to me, beacuse I'm a new beginner  programmer
    

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Java developers know that testing code changes can be a huge pain, and waiting for an application to redeploy after a code fix can take an eternity. Wouldn't it be great if you could see your code changes immediately, fine-tune, debug, explore and deploy code without waiting for ages? In this white paper, find out how that's possible with a Java plugin that drastically changes the way you develop, test and run Java applications. Discover the advantages of this plugin, and the changes you can expect to see …

  • Live Event Date: August 14, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT Data protection has long been considered "overhead" by many organizations in the past, many chalking it up to an insurance policy or an extended warranty you may never use. The realities of today make data protection a must-have, as we live in a data driven society. The digital assets we create, share, and collaborate with others on must be managed and protected for many purposes. Check out this upcoming eSeminar and join eVault Chief Technology …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds