Mega Code Archive

 
Categories / Delphi / Examples
 

Word Count

Title: Word Count Question: How can I count words in a string? Answer: To count the number of words in a string, insert the following code into your unit, and call it. Function Seps(As_Arg: Char): Boolean; Begin Seps := As_Arg In [#0..#$1F, ' ', '.', ',', '?', ':', ';', '(',')', '/', '\']; End; Function Word_Count(CText: String): Longint; Var Ix: Word; Work_Count: Longint; Begin Work_Count := 0; Ix := 1; While Ix While (Ix Inc(Ix); If Ix Inc(Work_Count); While (Ix Inc(Ix); End; End; Word_Count := Work_Count; End; To call it, do something like this: Word_Count('How many words in this sentance?'); You can modify which characters are counted as words.