Count number of words in a string

With this scripting code, you can count how many words a particular string has. Two functions, namely CountWordsA and CountWordsB are provided to accomplish that. Try the one you feel most comfortable with.

Although both function may appear to be similar, the way they determine number of words in a string is slightly different. Both functions iterate, one character at a time, through the string (the parameter you pass) to count the number of words in a string. No we cannot just count space characters in a string to find out how many words we have because each string may contain more than one space character. We rather need a different approach. The first function, CountWordsA, assumes that you want to count a "word" as a "word" if the current character is a space and the next character is not. That eliminates the possibility of counting words that could be separated by more than one space character as any consecutive space characters (specifically, the current and the next space character) would not be ignored.

On the other hand, the second function, CountWordsB, compares the current and the previous characters in a string. If they both are space characters, then, it is not a word. If, however, the current character is a space character and the previous character was not, then, we know it is a word.

Scripting code

<%
dim aString
aString = "a b c d e "

' count words and display the result
response.write "Number of words returned from CountWordsA (): " & CountWordsA (aString)
response.write "<br>Number of words returned from CountWordsB (): " & CountWordsB (aString)

' the function counts number of words in a string.
function CountWordsA (str)
str = trim (str) ' remove spaces, if any, from both sides of string
dim intTotalWords, intStrLength
intStrLength = len(str) ' store the number of characters in the string
intTotalWords = 1
if intStrLength = 0 then ' check if the string is empty
intTotalWords = 0 ' 0 words if the string is empty
else ' string is not empty
dim i, strCurChar, stNextChar
' start from the first character in the string.
' compare the current character with the next (i+1).
' if the current character is space and the next is not, then it is a word,
' If the string has only one character, loop does not execute
' in this case intTotalWords = 1 is returned
for i = 1 to intStrLength - 1
strCurChar = mid(str, i, 1) ' current character
stNextChar = mid(str, i+1, 1) ' next character
' if the current chracter is a space and the next character
' is not a space, then count that as a word.
if strCurChar = Chr(32) and stNextChar <> Chr(32) then
intTotalWords = intTotalWords + 1
end if
next
end if
' return number of words
CountWordsA = intTotalWords
end function

' the function counts number of words in a string.
function CountWordsB (str)
str = trim (str) ' remove spaces, if any, from both sides of string
dim intTotalWords, intStrLength
intStrLength = len(str) ' store the number of characters in the string
intTotalWords = 1
if intStrLength = 0 then ' check if the string is empty
intTotalWords = 0 ' 0 words if the string is empty
else ' string is not empty
dim i, strCurChar, strPrevChar
' start from the second character in the string
' Compare the current character with the previous (i -1) character.
' If the string has only one character, loop does not execute
for i = 2 to intStrLength
strCurChar = mid(str, i, 1) ' current character
strPrevChar = mid(str, i-1, 1) ' previous character
' if the current character is a space character and the previous
' is not, then count that as a word.
if strCurChar = Chr(32) and strPrevChar <> Chr(32) then
intTotalWords = intTotalWords + 1
end if
next
end if
' return number of words
CountWordsB = intTotalWords
end function
%>

Output of the above code

Number of words returned from CountWordsA (): 5
Number of words returned from CountWordsB (): 5