Author: Lou Adler
How to count the number of words in a string
Answer:
1 { ... }2 type3 TCharSet = TSysCharSet;
4 { ... }5 6 function WordCount(const S: string; const WordDelims: TCharSet): Integer;
7 var8 SLen, I: Cardinal;
9 begin10 Result := 0;
11 I := 1;
12 SLen := Length(S);
13 while I <= SLen do14 begin15 while (I <= SLen) and (S[I] in WordDelims) do16 Inc(I);
17 if I <= SLen then18 Inc(Result);
19 while (I <= SLen) andnot (S[I] in WordDelims) do20 Inc(I);
21 end;
22 end;
23 24 //Use the following statement to call the function:25 26 WordCount(Edit1.Text, [' ', ','])