Word Count Help !!

Please support our Visual Basic 4 / 5 / 6 advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Feb 2007
Posts: 16
Reputation: Kcin is an unknown quantity at this point 
Solved Threads: 1
Kcin Kcin is offline Offline
Newbie Poster

Word Count Help !!

 
0
  #1
Feb 25th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 848
Reputation: QVeen72 is on a distinguished road 
Solved Threads: 120
QVeen72's Avatar
QVeen72 QVeen72 is offline Offline
Practically a Posting Shark

Re: Word Count Help !!

 
0
  #2
Feb 26th, 2007
Hi,

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

Regards
Veena
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 51
Reputation: dmf1978 is an unknown quantity at this point 
Solved Threads: 7
dmf1978's Avatar
dmf1978 dmf1978 is offline Offline
Junior Poster in Training

Re: Word Count Help !!

 
0
  #3
Feb 26th, 2007
I hope it could help.
  1. Private Function CountWords(strText As String) As Long
  2. Dim x As Long
  3. Dim words As Long
  4. Dim lenStr As Long
  5. Dim lenPrevWord As Long
  6. Dim currentAscii As Integer
  7.  
  8. ' Number of chars in string
  9. lenStr = Len(strText)
  10. ' Number of chars in the last word processed
  11. lenPrevWord = 0
  12.  
  13. For x = 1 To lenStr
  14. ' ASCII value of the char being processed
  15. currentAscii = Asc(Mid$(strText, x, 1))
  16. Select Case currentAscii
  17. ' If current char is space (ASCII value 32)...
  18. Case 32
  19. ' ...and we have processed at least a char before
  20. ' then we have found a word
  21. If lenPrevWord > 0 Then
  22. words = words + 1
  23. ' Clear count of characters in previous word
  24. lenPrevWord = 0
  25. End If
  26. Case Else
  27. ' If current char is not a space, then add 1 to the
  28. ' count of chars of the current word.
  29. lenPrevWord = lenPrevWord + 1
  30. End Select
  31.  
  32. ' Test if last char is other than a space.
  33. ' If so then there is a word.
  34. If x = lenStr And currentAscii <> 32 Then
  35. words = words + 1
  36. End If
  37. Next x
  38.  
  39. CountWords = words
  40. End Function
Bye!
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 51
Reputation: dmf1978 is an unknown quantity at this point 
Solved Threads: 7
dmf1978's Avatar
dmf1978 dmf1978 is offline Offline
Junior Poster in Training

Re: Word Count Help !!

 
0
  #4
Feb 26th, 2007
I have uploaded a sample VB6 project to demonstrate the use of this function.
Attached Files
File Type: zip CountWords.zip (2.6 KB, 62 views)
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 16
Reputation: Kcin is an unknown quantity at this point 
Solved Threads: 1
Kcin Kcin is offline Offline
Newbie Poster

Re: Word Count Help !!

 
0
  #5
Feb 26th, 2007
Originally Posted by dmf1978 View Post
I have uploaded a sample VB6 project to demonstrate the use of this function.
sadly Your program does not work.
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 16
Reputation: Kcin is an unknown quantity at this point 
Solved Threads: 1
Kcin Kcin is offline Offline
Newbie Poster

Re: Word Count Help !!

 
0
  #6
Feb 26th, 2007
Originally Posted by QVeen72 View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 51
Reputation: dmf1978 is an unknown quantity at this point 
Solved Threads: 7
dmf1978's Avatar
dmf1978 dmf1978 is offline Offline
Junior Poster in Training

Re: Word Count Help !!

 
0
  #7
Feb 26th, 2007
Originally Posted by Kcin View Post
sadly Your program does not work.
Hi! Tell me why my program doesnt work so I can fix it.
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 16
Reputation: Kcin is an unknown quantity at this point 
Solved Threads: 1
Kcin Kcin is offline Offline
Newbie Poster

Re: Word Count Help !!

 
0
  #8
Feb 26th, 2007
Originally Posted by dmf1978 View Post
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(CountWords(txtText.Text)) & " words."
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 51
Reputation: dmf1978 is an unknown quantity at this point 
Solved Threads: 7
dmf1978's Avatar
dmf1978 dmf1978 is offline Offline
Junior Poster in Training

Re: Word Count Help !!

 
0
  #9
Feb 26th, 2007
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?
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 848
Reputation: QVeen72 is on a distinguished road 
Solved Threads: 120
QVeen72's Avatar
QVeen72 QVeen72 is offline Offline
Practically a Posting Shark

Re: Word Count Help !!

 
0
  #10
Feb 27th, 2007
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:
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC