PC Review


Reply
Thread Tools Rate Thread

How to count words in a string

 
 
Tom E.
Guest
Posts: n/a
 
      14th Dec 2003
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!
 
Reply With Quote
 
 
 
 
One Handed Man [ OHM# ]
Guest
Posts: n/a
 
      14th Dec 2003
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






Tom E. wrote:
> 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 Removed)


 
Reply With Quote
 
Cor
Guest
Posts: n/a
 
      14th Dec 2003
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


"Tom E." <(E-Mail Removed)> schreef in bericht
news:1398401c3c24c$d87b2d00$(E-Mail Removed)...
> 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!



 
Reply With Quote
 
Herfried K. Wagner [MVP]
Guest
Posts: n/a
 
      14th Dec 2003
* "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".

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
 
Reply With Quote
 
One Handed Man [ OHM# ]
Guest
Posts: n/a
 
      14th Dec 2003
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#

Herfried K. Wagner [MVP] wrote:
> * "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 Removed)


 
Reply With Quote
 
Tom E.
Guest
Posts: n/a
 
      14th Dec 2003
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#
>
>Herfried K. Wagner [MVP] wrote:
>> * "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 Removed)
>
>
>.
>

 
Reply With Quote
 
Herfried K. Wagner [MVP]
Guest
Posts: n/a
 
      14th Dec 2003
* "One Handed Man [ OHM# ]" <OneHandedMan@&REMOVE&TO%MAIL%MEBTInternet.com> scripsit:
> 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.

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
 
Reply With Quote
 
Cor
Guest
Posts: n/a
 
      14th Dec 2003
Hi,

My example has also an error I saw.

Cor


 
Reply With Quote
 
Herfried K. Wagner [MVP]
Guest
Posts: n/a
 
      14th Dec 2003
* "Cor" <(E-Mail Removed)> scripsit:
> My example has also an error I saw.


;-)

Maybe an improved algorithm for word counting can be found somewhere on
the web...

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
 
Reply With Quote
 
Cor
Guest
Posts: n/a
 
      15th Dec 2003
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




"


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ms access: how count # words in a string arvintheoc Microsoft Access 1 12th Jun 2009 09:52 PM
Re: ms access: how count # words in a string Ken Snell [MVP] Microsoft Access 0 12th Jun 2009 08:57 PM
Re: ms access: how count # words in a string John Spencer MVP Microsoft Access 0 12th Jun 2009 08:53 PM
Count of words in Word 2007 and count in 2003 are different Gabi Microsoft Word Document Management 2 27th May 2008 12:20 PM
Can I set word count to not count words with three letters less? =?Utf-8?B?RGlhbmFIb2xtZXM=?= Microsoft Word Document Management 8 24th Sep 2006 05:53 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:38 PM.