Results 1 to 15 of 15

Thread: help with word and character counter

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Aug 2005
    Location
    perth
    Posts
    30

    Question help with word and character counter

    hihi

    i have to write some visual basic to make a word counter, character counter and count the commas and full stops too.
    but i only know how to do the word counter!! can anyone help with the character counter (how many characters altogether in a paragraph) or the other two!! thanks

  2. #2
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: help with word and character counter

    VB Code:
    1. msg len(text1.text)

    will count the characters.

  3. #3
    Frenzied Member Devion's Avatar
    Join Date
    Sep 2000
    Location
    The Netherlands
    Posts
    1,049

    Re: help with word and character counter

    VB Code:
    1. Public Function fCountOccurance(strData as string, strChar as string) as Long
    2.      
    3.      Dim CharCount as long
    4.  
    5.      CharCount = 0 ' reset counter
    6.    
    7.      For X = 1 To Len(StrData) 'loop through word
    8.           If Mid$(Str, X, 1) = strChar Then
    9.                CharCount = CharCount + 1 'increase the counter by one
    10.           End If
    11.      Next
    12.  
    13.     fCountOccurance = CharCount
    14. End function

    Occurance counter The Easy Way(tm)(c) .. The one did without having any coffee yet -.-

  4. #4
    Fanatic Member vbasicgirl's Avatar
    Join Date
    Jan 2004
    Location
    Manchester, UK
    Posts
    1,016

    Re: help with word and character counter

    I have seen this tested for speed somewhere and if i remember rightly, InStr() came out on top.
    I use to use this
    VB Code:
    1. Private Function CountChar(MainString As String, TheChar As String) As Long
    2.   CountChar = Len(MainString) - Len(Replace(MainString, TheChar, ""))
    3. End Function
    But i believe this is faster
    VB Code:
    1. Private Function CountChar(MainString As String, TheChar As String) As Long
    2. Dim pos As Long
    3.  pos = InStr(1, MainString, TheChar)
    4.  
    5.   Do While pos
    6.    CountChar = CountChar + 1
    7.    pos = InStr(pos + 1, MainString, TheChar)
    8.   Loop
    9. End Function

    casey.

  5. #5
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: help with word and character counter

    Word counter...

    VB Code:
    1. Dim strWords() As String
    2. Dim bReplaced As Boolean
    3. Dim strTestData As String
    4.  
    5. strTestData = "This is  some test     data with extra spaces in it" _
    6.             & " and it contains fifteen words."
    7.            
    8. ' Get rid of the double, triple, etc spaces.
    9. bReplaced = True
    10. Do Until Not bReplaced
    11.     bReplaced = False
    12.     ' Look for double spaces
    13.     If InStr(strTestData, "  ") Then
    14.         ' Replace the double spaces with single spaces
    15.         strTestData = Replace(strTestData, "  ", " ")
    16.         bReplaced = True
    17.     End If
    18. Loop
    19.  
    20. ' Now see how many words there are
    21. strWords = Split(strTestData, " ")
    22. ' We add one because strWords(0) is the 1st word
    23. MsgBox "There are " & UBound(strWords) + 1 & " words"

  6. #6
    Frenzied Member Devion's Avatar
    Join Date
    Sep 2000
    Location
    The Netherlands
    Posts
    1,049

    Re: help with word and character counter

    Martin.. your code would actually count dashes and any other loose character

  7. #7
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: help with word and character counter

    Quote Originally Posted by Devion
    Martin.. your code would actually count dashes and any other loose character
    You're right I hadn't thought of that. Interestingly I just tried Word to see what it does and it says "This - is a test" has 4 words but "This -- is a test" has 5, so if I wanted to modify my code to work like Word I guess I would look for single characters in the strWord() array that weren't letters and ignore them.

  8. #8
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: help with word and character counter

    VB Code:
    1. Dim str1 As String
    2. str1 = Text1.Text
    3. Do While InStr(str1, "  ") > 0
    4.     str1 = Replace(str1, "  ", " ")
    5. Loop
    6. 'To count no. of words
    7. MsgBox Len(str1) - Len(Replace(str1, " ", "")) + 1
    8.  
    9. 'To count no. of characters (with spaces)
    10. MsgBox Len(str1)
    11.  
    12. 'To count no. of characters (without spaces)
    13. MsgBox Len(Replace(str1, " ", ""))
    14.  
    15. 'To count no. of semi-colons or any other character
    16. MsgBox Len(str1) - Len(Replace(str1, ";", ""))
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  9. #9
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: help with word and character counter

    I not sure this is rigorous enough but you get the idea.

    VB Code:
    1. Dim strWords() As String
    2. Dim bReplaced As Boolean
    3. Dim strTestData As String
    4. Dim lngIndex As Long
    5. Dim intCount As Integer
    6.  
    7. strTestData = "This is  a test  -  string with extra spaces in it" _
    8.             & " and it contains fifteen words."
    9.            
    10. ' Get rid of the double, triple, etc spaces.
    11. bReplaced = True
    12. Do Until Not bReplaced
    13.     bReplaced = False
    14.     ' Look for double spaces
    15.     If InStr(strTestData, "  ") Then
    16.         ' Replace the double spaces with single spaces
    17.         strTestData = Replace(strTestData, "  ", " ")
    18.         bReplaced = True
    19.     End If
    20. Loop
    21.  
    22. ' Now see how many words there are
    23. strWords = Split(strTestData, " ")
    24.  
    25. ' We add one because strWords(0) is the 1st word
    26. intCount = UBound(strWords) + 1
    27.        
    28.  
    29. For lngIndex = 0 To UBound(strWords)
    30.     If Len(strWords(lngIndex)) = 1 Then
    31.         If InStr(1, "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", UCase(strWords(lngIndex))) Then
    32.            ' The above are OK
    33.         Else
    34.             intCount = intCount - 1
    35.         End If
    36.     End If
    37. Next
    38.            
    39. MsgBox "There are " & intCount & " words"

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Aug 2005
    Location
    perth
    Posts
    30

    Re: help with word and character counter

    THANKS!! that was a little simple but combined with the other replies i think i've got it! cheers

  11. #11

    Thread Starter
    Junior Member
    Join Date
    Aug 2005
    Location
    perth
    Posts
    30

    Re: help with word and character counter

    for this you are using a test piece of string right??

    if i wanted to make that a paragraph of text i have inputted what would i do??

    thanks for your help so far

  12. #12
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: help with word and character counter

    VB Code:
    1. Public Function CountWords(strText As String) As Integer
    2.  
    3. Dim strWords() As String
    4. Dim bReplaced As Boolean
    5. Dim lngIndex As Long
    6.            
    7. ' Get rid of the double, triple, etc spaces.
    8. bReplaced = True
    9. Do Until Not bReplaced
    10.     bReplaced = False
    11.     ' Look for double spaces
    12.     If InStr(strText, "  ") Then
    13.         ' Replace the double spaces with single spaces
    14.         strText = Replace(strText, "  ", " ")
    15.         bReplaced = True
    16.     End If
    17. Loop
    18.  
    19. ' Now see how many words there are
    20. strWords = Split(strText, " ")
    21.  
    22. ' We add one because strWords(0) is the 1st word
    23. CountWords = UBound(strWords) + 1
    24.  
    25. For lngIndex = 0 To UBound(strWords)
    26.     If Len(strWords(lngIndex)) = 1 Then
    27.         If InStr(1, "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", UCase(strWords(lngIndex))) Then
    28.            ' The above are OK
    29.         Else
    30.             CountWords = CountWords - 1
    31.         End If
    32.     End If
    33. Next
    34.  
    35. End Function

    Usage
    VB Code:
    1. MsgBox CountWords(<Your string or variable name goes here>)

    BTW, welcome to VB Forums!

  13. #13

    Thread Starter
    Junior Member
    Join Date
    Aug 2005
    Location
    perth
    Posts
    30

    Re: help with word and character counter

    i dont know much about VB but you code looks easy enough to understand

    however, i dont know what i am replacing with what..eg. my text boxs and command buttons have names but where should i be putting the names of each into my code??

  14. #14

    Thread Starter
    Junior Member
    Join Date
    Aug 2005
    Location
    perth
    Posts
    30

    Re: help with word and character counter

    and if i put that code in straight away will it work? because it doesnt seem to..i think i need to chnage something to fit into mine..i just dont know what (i am not very experienced AT all with VB).

  15. #15
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: help with word and character counter

    In the click event of the button that you want to count words, place something like this:

    VB Code:
    1. MsgBox CountWords(myTextbox.text) & " words in document"

    or to place them in a variable,
    VB Code:
    1. dim x as integer
    2. x = CountWords(myTextbox.text)
    3. msgbox("There are " & x & " words in this document")

Posting Permissions

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



Click Here to Expand Forum to Full Width