Question about Compilers & Programming Tools

2 Answers

Sentence can you help me to make my vb project run? my problem is "what code shall i use in displaying number of words as i enter a sentence" thank you..your code can help a lot to run my program.. god bless...

Posted by on

2 Answers

  • Level 1:

    An expert who has achieved level 1.


    An expert that got 10 achievements.


    An expert that got 5 achievements.


    An expert who has written 3 tips or uploaded 2 video tips.

  • Contributor
  • 51 Answers

Assuming you define "word" as any string of non-whitespace characters bounded by a whitespace character (ie, space, tab, carriage return), or if in first position bounde donly rightwise and if in last position bounded only leftwise, then you would want to use the On_KeyUp event in the writing object in question (will assume it's a TextBox). I won't write the code for you but will provide pseudo-code:

private whitespaces, wordcount as Integer ' <--- Note: These are module-level vars


if [typed character is whitespace and wordcount = 0] then
wordcount = wordcount + 1
elseif [typed character is whitespace and wordcount > 0] then
if [whitespaces > 0 then]
wordcount = wordcount + 1
whitespaces = 0
whitespaces = 1
end if
end if


COMMENTS: Notice that if a typed char is not a whitespace we don't care much about it. The whitespace is defining the word-count, not the actual words themselves. The approach to take with stuff like this is not to think of the problem the way a human naturally does (ie, we count words we see on a piece of paper or screen and say what we saw), but to ask, "How is 'word' defined?", then based on that definition, ask how you can spot when a differentiation between words is detectable at any given time in the history of the object's state. In this case it is known by the entering of whitespace chars and their relation to one another, so that is what we are concerned with.

One thing this code above does not do is handle the terminal case, for example, if I type:

"All good people turn their heads each day"

in a textbox, the count would be 7, since I am not catching the termination point of the word "day". If I had typed "All good people turn their heads each day ", then I would have gotten a count of 8. Also, if I type ". . ." have I just entered 3 words? No, of course not, but the code above would say so (as long as I handled the terminal case; see below).

So what you must also handle for is the terminal case and the only way to know it has been reached is to add a count value when the user indicates he or she is done typing, most likely by clicking off the box (use the object's lost focus event). If the application you are building though is keeping the word count limited, then when a user enters a whitespace and causes the count to go above a certain number, then you cancel the key event and revert the text. This requires more coding as well, and use of the previous value property of the object, for example, or use of string functions like Left() applied to the text in the text entry object you are using. Another thing is that this code does not handle deletes and backspaces; what happens when I hit backspace or select and delete a word of set of words? Now things have gotten really fun.

As you can see, this is not as easy as it first looks, as with all event-oriented, user-centric programming. The matter of keeping track of the state of the application and its data (both on-screen and in databases) is always more complicated than it first appears and so it is better to come up with a state-management strategy ahead of time. Just be prepared to have that strategy become either useless or make exceptions to it, as in an app of any size, the chances of that happening are about 99.9999%. There is also making sure your definitions for things like "word" are accurate to the purpose at hand; in some cases, you might want a string like " . " to be considered a 'word' and at other times, not. The devil is in the details.

Having said that an I am certain thoroughly discouraged you, all i can say is, Good Luck!

Posted on Feb 27, 2008

Send me your code -- and I can fix it

Posted on Feb 27, 2008

Add Your Answer

Uploading: 0%


Complete. Click "Add" to insert your video. Add



Related Questions:

1 Answer

We want to make mini project in vb and we want that it is a veriety and useful and also in less expence

try to this topic. HOW TO CONVERT NUMBERS INTO WORDS. you can search though internet the free program for converting numbers into words.

Dec 23, 2010 | Microsoft Visual Basic 6.0 for PC

2 Answers

Source code for word count

Haha, i wrote one of thee a while ago for a competition. You need to us e the char at function mate :D
Inside a for loop
Ill havea look for you though :D

Sep 09, 2008 | ArcMedia JavaScript Source Code 3000 Pro...

1 Answer

Formatting/Rounding number

forget its not your cup of tea

Jul 31, 2008 | Compilers & Programming Tools

1 Answer


Sub aa()
' aa Macro
Dim a As String
a = "how are you this is dharani"
Dim leng, cnt As Integer
leng = 1
cnt = 0
While leng <= Len(RTrim(a))
If Mid(a, leng, 1) = " " Then
cnt = cnt + 1
End If
leng = leng + 1
If Len(RTrim(a)) > 0 Then
cnt = cnt + 1
End If
MsgBox Str(cnt) + " Words"
End Sub

Feb 27, 2008 | Compilers & Programming Tools

2 Answers

Visual basic 6.0

Use a compare (instr) command to search the sentence for punctution and spaces. Increment a variable for each word found, resetting the search string and retaining the start position of each find.

Feb 27, 2008 | Compilers & Programming Tools

1 Answer


i did not solve my problem yesterday.
that's why i need your help..
please help me to solve my problem that i posted yesterday.i need your help.
thank you very much.

Feb 21, 2008 | Compilers & Programming Tools

2 Answers

Help me

this the code for your problem. by the way this for vb6.0

Sub CountCharacters(Text As String)
Dim iVowel As Integer
Dim iCons As Integer
Dim iNum As Integer
Dim iSpace As Integer
Dim iOther As Integer

Dim cnt As Integer
Dim sChar As String

For cnt = 1 To Len(Text)
sChar = LCase(Mid(Text, cnt, 1))

Select Case sChar
Case "a", "e", "i", "o", "u" ' And sometimes "y"?
' Vowel
iVowel = iVowel + 1
Case "a" To "z"
' Includes the first case, but VB matches the first case and stops
iCons = iCons + 1
Case "0" To "9"
' Numbers
iNum = iNum + 1
Case " ", vbTab, vbLf
' Space characters
' Not including vbCr as this will likely be dealing with Windows text
' and I only want to match one character for a newline
iSpace = iSpace + 1
Case Else
If sChar <> vbCr Then iOther = iOther + 1
End Select
Next cnt

Debug.Print "Text: " & Text
Debug.Print "Vowels: " & CStr(iVowel)
Debug.Print "Consonants: " & CStr(iCons)
Debug.Print "Numbers: " & CStr(iNum)
Debug.Print "Space characters: " & CStr(iSpace)
Debug.Print "Other: " & CStr(iOther)
End Sub

Feb 21, 2008 | Compilers & Programming Tools

Not finding what you are looking for?
Compilers & Programming Tools Logo

Related Topics:

103 people viewed this question

Ask a Question

Usually answered in minutes!

Top Compilers & Programming Tools Experts

Robin Hill
Robin Hill

Level 3 Expert

1251 Answers

Fred Block
Fred Block

Level 2 Expert

289 Answers

Les Dickinson
Les Dickinson

Level 3 Expert

18299 Answers

Are you a Compiler and Programming Tool Expert? Answer questions, earn points and help others

Answer questions

Manuals & User Guides