Compter les mots d'une chaine
function o_WordsCount( const _Texte: string ): Integer;
const
sep: set of char = ' ' , '.', ':', ';', '"', '!', '?', ',', '(', ')', '{', '}', '[',
']', '=', '@', '$', '|', '§', '&', '*', '£', '/', '', #9, #13,
#10 ];
tir: set of char = [ '-' , '''' ];
var
x: integer;
mot: string;
begin
mot := '';
Result := 0;
x := 1;
while x < length( _Texte ) + 1 do begin
{ Supprimer d'abord les tirets isolés '-' pour conserver les noms composés
(évidemment les formes conjuguées avec un tiret joignant les deux mots seront
comptabilisées comme un seul mot).
Supprimer également les apostrophes ' multiples. }
if _Texte[ x ] = '-' then begin
repeat
x:= x + 1;
until not ( _Texte[ x + 1 ] in tir ) and not ( _Texte[ x + 1 ] in sep );
end;
{ Remplacer tous les espaces, signes de ponctuation et caractères exotiques }
if _Texte[ x ] in sep then begin
if mot <> '' then begin
Result := Result + 1;
mot := '';
end;
end
else
mot := mot + _Texte[ x ];
x := x + 1;
end;
end;
( extrait de o_fonctions :
http://www.fobec.com/protec/grenier2/encadre.php3?cat=VUnité&id_log=35 )