954,558 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Word Count Help !!

OK, first of all, thank you for coming trying to help.
anyways, i need help on making a program that counts how many words you have typed.
REstriction: Must use Strings...

Please help me, thx in advance.;)

Kcin
Newbie Poster
16 posts since Feb 2007
Reputation Points: 9
Solved Threads: 1
 

Hi,

U need to count words from a textbox or a text file or a Word file..?

Regards
Veena

QVeen72
Posting Shark
950 posts since Nov 2006
Reputation Points: 84
Solved Threads: 143
 

I hope it could help.

Private Function CountWords(strText As String) As Long
    Dim x As Long
    Dim words As Long
    Dim lenStr As Long
    Dim lenPrevWord As Long
    Dim currentAscii As Integer
    
    ' Number of chars in string
    lenStr = Len(strText)
    ' Number of chars in the last word processed
    lenPrevWord = 0
    
    For x = 1 To lenStr
        ' ASCII value of the char being processed
        currentAscii = Asc(Mid$(strText, x, 1))
        Select Case currentAscii
            ' If current char is space (ASCII value 32)...
            Case 32
                ' ...and we have processed at least a char before
                ' then we have found a word
                If lenPrevWord > 0 Then
                    words = words + 1
                    ' Clear count of characters in previous word
                    lenPrevWord = 0
                End If
            Case Else
                ' If current char is not a space, then add 1 to the
                ' count of chars of the current word.
                lenPrevWord = lenPrevWord + 1                                     
        End Select

        ' Test if last char is other than a space.
        ' If so then there is a word.
        If x = lenStr And currentAscii <> 32 Then
            words = words + 1
        End If
    Next x
    
    CountWords = words
End Function

Bye!

dmf1978
Junior Poster in Training
51 posts since Aug 2006
Reputation Points: 18
Solved Threads: 7
 

I have uploaded a sample VB6 project to demonstrate the use of this function.

Attachments CountWords.zip (2.6KB)
dmf1978
Junior Poster in Training
51 posts since Aug 2006
Reputation Points: 18
Solved Threads: 7
 
I have uploaded a sample VB6 project to demonstrate the use of this function.



sadly Your program does not work.:sad:
but just now i came up with something:
Private Sub cmd_Click()
txt = txt.Text
txt.Text = Trim$(txt.Text)
spacecount = 1
For x = 1 To Len(txt)
If Mid(txt, x, 1) = " " Then spacecount = spacecount + 1
Next
MsgBox spacecount & " words found."
End Sub

This program does not count the number of words but the number of spaces. This will work too if i can just get it to stop counting the extra spaces as words betweeen words.

Kcin
Newbie Poster
16 posts since Feb 2007
Reputation Points: 9
Solved Threads: 1
 
Hi, U need to count words from a textbox or a text file or a Word file..? Regards Veena


I need to count the words from a text box :)
It has to be able to not count 2 or more spaces between words.

Kcin
Newbie Poster
16 posts since Feb 2007
Reputation Points: 9
Solved Threads: 1
 
sadly Your program does not work.:sad:

Hi! Tell me why my program doesnt work so I can fix it.

dmf1978
Junior Poster in Training
51 posts since Aug 2006
Reputation Points: 18
Solved Threads: 7
 
Hi! Tell me why my program doesnt work so I can fix it.



Your need to define CountWords
Private Sub txtText_Change()
lblWords.Caption = "You have tiped " & CStr(<strong>CountWords</strong>(txtText.Text)) & " words."

Kcin
Newbie Poster
16 posts since Feb 2007
Reputation Points: 9
Solved Threads: 1
 

CountWords is the function coded inside the module "mdlWordCount.bas".

Did you open de prjWordCount.vbp file to load the project in the VB6 IDE or just the frm file?

dmf1978
Junior Poster in Training
51 posts since Aug 2006
Reputation Points: 18
Solved Threads: 7
 

Hi,

Just use this simple Code:

Private Sub CmdCount_Click()
    Dim MyCntArray
    Dim MyStr As String
    Dim i As Integer
    '
    MyStr = TextBox1.Text
    '
    For i = 2 To 10
        MyStr = Replace(MyStr, Space(i), Space(1))
    Next
    ' Replace All the multiple Spaces with single spaces 
    ' here i have given for max 10 spaces, 
    ' u can change it to a higher value
    '
    MyCntArray = Split(MyStr, " ")
    MsgBox "Number Of Words : " & UBound(MyCntArray) - 1
    '
End Sub


I hope it is clear.

Regards
Veena :lol:

QVeen72
Posting Shark
950 posts since Nov 2006
Reputation Points: 84
Solved Threads: 143
 

Hi,

Sorry, Little bit of Modification in my previous post :
Just Change For Loop this way :

For i = 10 To 2 Step -1
        MyStr = Replace(MyStr, Space(i), Space(1))
    Next
    MyCntArray = Split(MyStr, " ")
    MsgBox UBound(MyCntArray)



Regards
Veena

QVeen72
Posting Shark
950 posts since Nov 2006
Reputation Points: 84
Solved Threads: 143
 

Hi, Sorry, Little bit of Modification in my previous post : Just Change For Loop this way :

For i = 10 To 2 Step -1
        MyStr = Replace(MyStr, Space(i), Space(1))
    Next
    MyCntArray = Split(MyStr, " ")
    MsgBox UBound(MyCntArray)
Regards Veena

Veena, you must be joking! :lol:
Don't you? :-|

Try this with your code "Hello world" and please post how many words your program find.

dmf1978
Junior Poster in Training
51 posts since Aug 2006
Reputation Points: 18
Solved Threads: 7
 

Veena, you must be joking! :lol: Don't you? :-|

Try this with your code "Hello world" and please post how many words your program find.

i got 1

Kcin
Newbie Poster
16 posts since Feb 2007
Reputation Points: 9
Solved Threads: 1
 

Hi,

Did u look into my last post???? I have corrected there.

MsgBox UBound(MyCntArray)


Remove "-1"

Regards

Veena

QVeen72
Posting Shark
950 posts since Nov 2006
Reputation Points: 84
Solved Threads: 143
 

Hi

This is My Complete. Code.

Private Sub CmdCount_Click()
    Dim MyCntArray
    Dim MyStr As String
    Dim i As Integer
    '
    MyStr = TextBox1.Text
    '
    For i = 10 To 2 Step -1
        MyStr = Replace(MyStr, Space(i), Space(1))
    Next
    ' Replace All the multiple Spaces with single spaces 
    ' here i have given for max 10 spaces, 
    ' u can change it to a higher value
    '
    MyCntArray = Split(MyStr, " ")
    MsgBox "Number Of Words : " & UBound(MyCntArray) 
    '
End Sub



I hope at least now, it is Clear.

Regards
Veena

QVeen72
Posting Shark
950 posts since Nov 2006
Reputation Points: 84
Solved Threads: 143
 

Thx Guys, I got it to work.
Thank you for all your help.

Love You all.

Kcin
Newbie Poster
16 posts since Feb 2007
Reputation Points: 9
Solved Threads: 1
 
I have uploaded a sample VB6 project to demonstrate the use of this function.


excellent code.
worked first time for me and is better than heaps of other examples.
Thanx 4 da code

arty56
Newbie Poster
9 posts since Apr 2007
Reputation Points: 10
Solved Threads: 0
 
excellent code. worked first time for me and is better than heaps of other examples. Thanx 4 da code


Hey, thanks :)

dmf1978
Junior Poster in Training
51 posts since Aug 2006
Reputation Points: 18
Solved Threads: 7
 
Hey, thanks :)


perhaps you have an answer for reading the total characters in a text box
and a way of limiting the number.

I've spent 93 mins so far and can not find a suitable answer.
Regards
Arty

arty56
Newbie Poster
9 posts since Apr 2007
Reputation Points: 10
Solved Threads: 0
 

perhaps you have an answer for reading the total characters in a text box and a way of limiting the number.

I've spent 93 mins so far and can not find a suitable answer. Regards Arty

Yes,
This gives you length of Characters in the TextBox..
MsgBox Len(Text1.Text)

You can Limit the TextBox Setting its MaxLength
Text1.maxLength =10

Regards
Veena

QVeen72
Posting Shark
950 posts since Nov 2006
Reputation Points: 84
Solved Threads: 143
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You