Saturday, October 11, 2008

WordCount

PHP has a function called str_word_count() which allows you to count the number of words in a string, and an array or associative array of those words. Unfortunately for us, ASP doesn't support optional parameters, so we can't duplicate all of this functionality in a single function, so we'll just concentrate on the main feature of counting the number of words.


ASP

  1. function WordCount(byVal someString)
  2.     dim position
  3.     dim spaces
  4.     spaces = 1
  5.     someString = trim(someString)
  6.     for position = 1 to len(someString)
  7.         if mid(someString, position, 1) = " " and not mid(someString, position - 1, 1) = " " then
  8.             spaces = spaces + 1
  9.         end if
  10.     next
  11.     WordCount = spaces
  12. end function

View ASP implementation on Snipplr

No comments: