Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 Other Forums
 Other Topics
 "word count" code

Author  Topic 

jenese
Starting Member

6 Posts

Posted - 2002-10-31 : 14:21:18
I'm working on a simple word count that takes a string (strNote)
and counts the number of spaces in that string, then returns number of spaces plus one as the total word count of the string.

It seems to work for shorter strings. It returns too small of a word count for strings over about 100 words.

Any suggestions?


------------------

strNumber = len(strNote)
strSpace = " "
strFound = 1
strPrev = 0
strNumCount = 0

strWordCount = (UBound( Split( strNotes, strSpace ) )) + 1

for n = 1 to strNumber
'count each space within strNote
strPrev = strFound' + 1
if cint(strFound) <> 0 then
strFound = instr(strPrev,strNote,strSpace,vbtextcompare)
if strFound > strPrev then
strNumCount = strNumCount + 1
strFound = strFound + 1 'increase so next is past this find
end if
end if
next
strNumCount = strNumCount + 1 'account for last word.
response.write("<i>" & strNumCount & " words</i>")

Page47
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2002-10-31 : 14:44:55
select datalength(strNote) - datalength(replace(strNote,' ','')) + 1

Jay White
{0}
Go to Top of Page

robvolk
Most Valuable Yak

15732 Posts

Posted - 2002-10-31 : 14:48:31
Well, I know this is Other Topics, but still it's not even REMOTELY connected to SQL Server...

How about this code?

wordCount = Len(strNotes)-Replace(strNotes, " ", "")+1

Hmmmm, that's a bit shorter.

BTW, a word about your variable names: Hungarian notation, which uses a prefix (str, int, etc.) to identify variables, is pretty archaic, and in fact you are using it incorrectly here:

strFound = strFound + 1

A string variable cannot perform mathematical functions like addition, and if it is not holding a string value then the variable should not have an "str" prefix. You'd be better off giving your variable descriptive names that do not rely on a prefix to describe their contents.

Go to Top of Page
   

- Advertisement -