Counting words in a string

# Thread: Counting words in a string

1. Senior Member
Join Date
Dec 1969
Posts
2,049

## Counting words in a string

Any ideas on how to do this...?<BR><BR>str = "one two three one one three two one two one"<BR><BR>*** Results<BR>(5) one<BR>(3) two<BR>(2) three<BR><BR>No idea how to start.<BR><BR>jc

2. Senior Member
Join Date
Dec 1969
Posts
96,118

## Convert to an array...

...sort the array, then count is easy.<BR><BR>Or you could use a dictionary object. But array probably faster.<BR><BR>

3. Senior Member
Join Date
Dec 1969
Posts
2,049

## RE: Convert to an array...

Is there a way to sort an array using vb or vbs only?<BR>(I noticed the ASP-FAQ uses Jscript)<BR><BR>I&#039;m going to put in a dll.<BR><BR>Thanks Bill.<BR><BR>jc

4. Senior Member
Join Date
Dec 1969
Posts
2,049

## RE: I found an article on it.

http://www.vb-helper.com/tut1.htm<BR><BR>Happy Holidays,<BR>jc

5. Senior Member
Join Date
Dec 1969
Posts
96,118

## Sure...how big an array?

If it&#039;s more than 100 elements or so, you should try to use a more advanced sort algorithm.<BR><BR>Under that size? Simple old bubble sort will do.<BR><BR>The *easiest* code is the dictionary scheme:<BR><BR>&#060;%<BR>arwords = Split( theText, " " )<BR><BR>Set words = Server.CreateObject("Scripting.Dictionary")<BR><BR >For wnum = 0 TO UBound(arwords)<BR> word = arwords(wnum)<BR> If word &#060;&#062; "" Then<BR> If words.Exists(word) Then<BR> words(word) = words(word) + 1<BR> Else<BR> words.Add word, 1<BR> End If<BR> End If<BR>Next<BR><BR>For Each word In words<BR> Response.Write word & ": " & words(word) & "&#060;br/&#062;"<BR>Next<BR>%&#062;<BR><BR>

6. Senior Member
Join Date
Dec 1969
Posts
2,049

## RE: 600 words max

Will bubble sort still work?

7. Senior Member
Join Date
Dec 1969
Posts
96,118

## That's pushing it...

It&#039;s probably worth using something else. Shell sort, maybe. Or heap sort.<BR><BR>How "mission critical" is this? How often will it be used?<BR><BR>

8. Senior Member
Join Date
Dec 1969
Posts
96,118

## So which do you want??

VB or VBS?<BR><BR>If VBS, and ASP, why would you *not* use the JS scheme??? It shows you how to cross-script to make it work.<BR><BR>If VB, is this VB6 or VB.NET? VB.NET has built-in array sort, I&#039;m reasonably sure.

9. Senior Member
Join Date
Dec 1969
Posts
2,049

## RE: That's pushing it...

I built a spider for a new search engine.<BR>My spider needs to sort out the words.<BR><BR>So the answer, a lot.<BR><BR>But I don&#039;t care if it&#039;s super fast.<BR>From what I read you do not want to make a spider too fast.<BR><BR>jc<BR><BR>

10. Senior Member
Join Date
Dec 1969
Posts
2,049

## RE: vb6

is what I&#039;m using, but vbs will work.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•