[fpc-pascal]Word count function

Gabor DEAK JAHN djg at tramontana.co.hu
Fri Sep 7 19:12:18 CEST 2001


At 9/6/01 08:07 AM, you wrote:

Jim,

 > Does anyone know of an efficient method for counting the words in a line of
 > data? I've written a few different functions, none of which were terribly

It also depends on what you exactly want, how you define words, whether you
need to treat quoted parts as a single word or not but basically, you simply
iterate over the string and count how many word separators you meet. To cope
with runs of more than one consequtive separator, you could simply use a
three state machine roughly like this:

var state : (insideword, firstseparator, moreseparator)

state := firstseparator
count := 0
for each character in the string do
   case state of
     firstseparator:
       increment (count)
       if character is separator then state := moreseparator
     moreseparator:
       if character is not separator then state := insideword
     insideword:
       if character is separator then state := firstseparator
if state = word then increment (count)


Bye,
    Gábor

-------------------------------------------------------------------
Gabor DEAK JAHN -- Budapest, Hungary.
E-mail: djg at tramontana.co.hu





More information about the fpc-pascal mailing list