Help Anyone? calculate words

lynn09

Member
Joined
Sep 15, 2004
Messages
17
Programming Experience
Beginner
i have a current project that the user must enter a sentence in the textbox and the program should automatically calculate the number of words and number of characters..help anyone?
 
You could use the Length property to count the number of characters in your String. You could look at counting "spaces" to help calculate the number of words. Validate your data, for example, number of spaces + 1 as the number of words, etc. But test test test your results!
 
I believe Neal just did :) ;) :confused: .

OK, I'll get you started. Here's a very simple solution. The answer may not be correct in all situations. As stated above, test test test your results!

VB.NET:
Dim str As String = "This is a test.  This is only a test."
Dim iChrs, iWords As Integer
'get the number of characters:
iChrs = str.Length
'remove the double spacing between sentences:
str = str.Replace("  ", " ")
'get the number of words:
iWords = str.Split(" ").Length
 
Lynn09,

We would like to see you attempt the code and not write it for you. Please make your attempts, post if you like, and we'll gladly assist you.
 
hi there..i have tried the coding but the number of character includes the spacing..could anyone help me on that?like hw to remove the spaces..i tested the program..for example..i typed in 5 empty spaces..the character shows 5 and the words show 4..
could anyone assist?asap?thanks



Dim str As String

Dim ichar, iWords, i As Integer

'get the number of characters:

str = T1.Text

ichar = str.Length

L1.Text = ichar

'remove the double spacing between sentences:

str = str.Replace(" ", " ")

'get the number of words:

iWords = str.Split(" ").Length

L2.Text = iWords

 
The line:
VB.NET:
str = str.Replace(" ", " ")
Should be:
VB.NET:
str = str.Replace("  ", " ") ' NOTE: Two Spaces for the first argument

That should at least get you closer.....

And because I'm feeling generous (and I don't believe that you could actually use this if it is a school project) you could also use RegularExpressions:

VB.NET:
   Function CountWords(ByVal Text As String) As Integer
        Dim r As Regex
        Dim m As Match
        Dim counter As Integer

        r = New Regex("\b\w+\b")

        m = r.Match(TextBox1.Text)
        While m.Success
            counter += 1
            m = m.NextMatch()
        End While

        Return counter
    End Function
 
Schenz,

tanks.but wad abt using e solution of
str = str.Replace(" ", " ") ' NOTE: Two Spaces for the first argument
its still counting the spaces in after i alterated it..hmm.
 
Are there two spaces in the first part of the line?

This messageboard seems to be taking the double space we're attempting to put there and making it just one space.
 
Schenz said:
Are there two spaces in the first part of the line?

This messageboard seems to be taking the double space we're attempting to put there and making it just one space.

ya..there is...but doesnt seem to work
 
ok, I played a bit more with it.

Try something like this:
VB.NET:
        Dim str As String
        Dim ichar, iWords As Integer

        'get the number of characters:
        str = T1.Text
        ichar = str.Length
        L1.Text = ichar

        'remove the double spacing between sentences:
        str = str.Replace("  ", " ")

        'get the number of words:
        Dim tempString As String = Trim(str.Replace("  ", " "))
        iWords = tempString.Split(" ").Length
        T2.Text = iWords
 
Back
Top