|
-
Aug 22nd, 2005, 11:19 PM
#1
Thread Starter
Junior Member
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
-
Aug 22nd, 2005, 11:49 PM
#2
Re: help with word and character counter
will count the characters.
-
Aug 23rd, 2005, 01:13 AM
#3
Frenzied Member
Re: help with word and character counter
VB Code:
Public Function fCountOccurance(strData as string, strChar as string) as Long
Dim CharCount as long
CharCount = 0 ' reset counter
For X = 1 To Len(StrData) 'loop through word
If Mid$(Str, X, 1) = strChar Then
CharCount = CharCount + 1 'increase the counter by one
End If
Next
fCountOccurance = CharCount
End function
Occurance counter The Easy Way(tm)(c) .. The one did without having any coffee yet -.-
-
Aug 23rd, 2005, 01:34 AM
#4
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:
Private Function CountChar(MainString As String, TheChar As String) As Long
CountChar = Len(MainString) - Len(Replace(MainString, TheChar, ""))
End Function
But i believe this is faster
VB Code:
Private Function CountChar(MainString As String, TheChar As String) As Long
Dim pos As Long
pos = InStr(1, MainString, TheChar)
Do While pos
CountChar = CountChar + 1
pos = InStr(pos + 1, MainString, TheChar)
Loop
End Function
casey.
-
Aug 23rd, 2005, 01:48 AM
#5
Re: help with word and character counter
Word counter...
VB Code:
Dim strWords() As String
Dim bReplaced As Boolean
Dim strTestData As String
strTestData = "This is some test data with extra spaces in it" _
& " and it contains fifteen words."
' Get rid of the double, triple, etc spaces.
bReplaced = True
Do Until Not bReplaced
bReplaced = False
' Look for double spaces
If InStr(strTestData, " ") Then
' Replace the double spaces with single spaces
strTestData = Replace(strTestData, " ", " ")
bReplaced = True
End If
Loop
' Now see how many words there are
strWords = Split(strTestData, " ")
' We add one because strWords(0) is the 1st word
MsgBox "There are " & UBound(strWords) + 1 & " words"
-
Aug 23rd, 2005, 02:11 AM
#6
Frenzied Member
Re: help with word and character counter
Martin.. your code would actually count dashes and any other loose character
-
Aug 23rd, 2005, 02:18 AM
#7
Re: help with word and character counter
 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.
-
Aug 23rd, 2005, 02:19 AM
#8
Re: help with word and character counter
VB Code:
Dim str1 As String
str1 = Text1.Text
Do While InStr(str1, " ") > 0
str1 = Replace(str1, " ", " ")
Loop
'To count no. of words
MsgBox Len(str1) - Len(Replace(str1, " ", "")) + 1
'To count no. of characters (with spaces)
MsgBox Len(str1)
'To count no. of characters (without spaces)
MsgBox Len(Replace(str1, " ", ""))
'To count no. of semi-colons or any other character
MsgBox Len(str1) - Len(Replace(str1, ";", ""))
-
Aug 23rd, 2005, 02:41 AM
#9
Re: help with word and character counter
I not sure this is rigorous enough but you get the idea.
VB Code:
Dim strWords() As String
Dim bReplaced As Boolean
Dim strTestData As String
Dim lngIndex As Long
Dim intCount As Integer
strTestData = "This is a test - string with extra spaces in it" _
& " and it contains fifteen words."
' Get rid of the double, triple, etc spaces.
bReplaced = True
Do Until Not bReplaced
bReplaced = False
' Look for double spaces
If InStr(strTestData, " ") Then
' Replace the double spaces with single spaces
strTestData = Replace(strTestData, " ", " ")
bReplaced = True
End If
Loop
' Now see how many words there are
strWords = Split(strTestData, " ")
' We add one because strWords(0) is the 1st word
intCount = UBound(strWords) + 1
For lngIndex = 0 To UBound(strWords)
If Len(strWords(lngIndex)) = 1 Then
If InStr(1, "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", UCase(strWords(lngIndex))) Then
' The above are OK
Else
intCount = intCount - 1
End If
End If
Next
MsgBox "There are " & intCount & " words"
-
Aug 23rd, 2005, 09:19 AM
#10
Thread Starter
Junior Member
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
-
Aug 23rd, 2005, 09:34 AM
#11
Thread Starter
Junior Member
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
-
Aug 23rd, 2005, 11:15 AM
#12
Re: help with word and character counter
VB Code:
Public Function CountWords(strText As String) As Integer
Dim strWords() As String
Dim bReplaced As Boolean
Dim lngIndex As Long
' Get rid of the double, triple, etc spaces.
bReplaced = True
Do Until Not bReplaced
bReplaced = False
' Look for double spaces
If InStr(strText, " ") Then
' Replace the double spaces with single spaces
strText = Replace(strText, " ", " ")
bReplaced = True
End If
Loop
' Now see how many words there are
strWords = Split(strText, " ")
' We add one because strWords(0) is the 1st word
CountWords = UBound(strWords) + 1
For lngIndex = 0 To UBound(strWords)
If Len(strWords(lngIndex)) = 1 Then
If InStr(1, "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", UCase(strWords(lngIndex))) Then
' The above are OK
Else
CountWords = CountWords - 1
End If
End If
Next
End Function
Usage
VB Code:
MsgBox CountWords(<Your string or variable name goes here>)
BTW, welcome to VB Forums!
-
Aug 25th, 2005, 01:50 AM
#13
Thread Starter
Junior Member
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??
-
Aug 25th, 2005, 01:56 AM
#14
Thread Starter
Junior Member
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).
-
Aug 25th, 2005, 02:14 AM
#15
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:
MsgBox CountWords(myTextbox.text) & " words in document"
or to place them in a variable,
VB Code:
dim x as integer
x = CountWords(myTextbox.text)
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|