How to count words in a string

  • Thread starter Thread starter Tom E.
  • Start date Start date
T

Tom E.

Hello, I would like to know what the most efficient way is to count
words in a string and to determine what the average word length is. I
know this can be done in a array, but is there an easier way using
VB.Net? Thank you in advance!
 
Simple version.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Dim str As String = "ABC DEF GHIJ KLMNOPQ"

Dim strA() As String = str.Split(" ")

Dim TChrs As Int32

Dim i As Int32

For i = strA.Length - 1 To 0 Step -1

TChrs += strA(i).Length

Next

MsgBox("Number of words = " & strA.Length)

MsgBox("Average Length = " & TChrs / strA.Length)

End Sub

Regards - OHM





Hello, I would like to know what the most efficient way is to count
words in a string and to determine what the average word length is. I
know this can be done in a array, but is there an easier way using
VB.Net? Thank you in advance!

Regards - OHM# (e-mail address removed)
 
Hi Tom,

If you dont have to long strings I would first look what split would do for
me.

Something like this (did not test it, just my first thought)
\\\\
dim mycount() as string = split(mystring)
myavarage = mystring.length / mycount.length
////

This is the Microsoft.Visual basic split, there is also a String.Split
method with wich you can give an array of delimiters and a regex.split.

If the string is very long than the split becomes slow, you can itterate
through the string.
(dont forget to compare characters than and not strings)

I hope this helps,

Cor
 
* "One Handed Man said:
Simple version.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Dim str As String = "ABC DEF GHIJ KLMNOPQ"

Dim strA() As String = str.Split(" ")

Dim TChrs As Int32

Dim i As Int32

For i = strA.Length - 1 To 0 Step -1

TChrs += strA(i).Length

Next

MsgBox("Number of words = " & strA.Length)

MsgBox("Average Length = " & TChrs / strA.Length)

End Sub

This method will not work with something like this: "Bla -- Foo" or "Foo---Bla". But
this is a rare case and maybe can be "ignored".
 
Thats why I said it was the 'Simple' version. Perhaps you could write
something more comprehensive yourself Herfried, I think the group would be
pleased to see some of your own work.

Regrards - OHM#
* "One Handed Man [ OHM# ]"
Simple version.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Dim str As String = "ABC DEF GHIJ KLMNOPQ"

Dim strA() As String = str.Split(" ")

Dim TChrs As Int32

Dim i As Int32

For i = strA.Length - 1 To 0 Step -1

TChrs += strA(i).Length

Next

MsgBox("Number of words = " & strA.Length)

MsgBox("Average Length = " & TChrs / strA.Length)

End Sub

This method will not work with something like this: "Bla -- Foo" or
"Foo---Bla". But this is a rare case and maybe can be "ignored".

Regards - OHM# (e-mail address removed)
 
Thanks for the ideas guys. I think this will help me out.

Tom
-----Original Message-----
Thats why I said it was the 'Simple' version. Perhaps you could write
something more comprehensive yourself Herfried, I think the group would be
pleased to see some of your own work.

Regrards - OHM#
* "One Handed Man [ OHM# ]"
<OneHandedMan@&REMOVE&TO%MAIL%MEBTInternet.com> scripsit:
Simple version.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Dim str As String = "ABC DEF GHIJ KLMNOPQ"

Dim strA() As String = str.Split(" ")

Dim TChrs As Int32

Dim i As Int32

For i = strA.Length - 1 To 0 Step -1

TChrs += strA(i).Length

Next

MsgBox("Number of words = " & strA.Length)

MsgBox("Average Length = " & TChrs / strA.Length)

End Sub

This method will not work with something like this: "Bla -- Foo" or
"Foo---Bla". But this is a rare case and maybe can be "ignored".

Regards - OHM# (e-mail address removed)


.
 
* "One Handed Man said:
Thats why I said it was the 'Simple' version. Perhaps you could write
something more comprehensive yourself Herfried, I think the group would be
pleased to see some of your own work.

Let's give the OP some homework. It was only a hint for the OP when
your method won't calculate the right value.
 
Hi Herfried,

But thinking it over, your example Foo--Bar is in the Dutch gramatic an
error so one word.
While Foo-Bar is normal Dutch gramatic and 1 word with 7 characters.

So the error from OHM is not really true, it depends on the used gramatic.

My error was that it had to be

(total length of character minis words) divided by words and I forgot the
amount of words to subract from the total length of characters.

:-)) Find if you can find an error in this?

Cor




"
 
* "Cor said:
But thinking it over, your example Foo--Bar is in the Dutch gramatic an
error so one word.
While Foo-Bar is normal Dutch gramatic and 1 word with 7 characters.

So the error from OHM is not really true, it depends on the used gramatic.

ACK. In French, they write "ashfdjd : asdasd", where in German we would
write "asdasd: asdasd". There are many other examples.
 
Back
Top