首页  编辑  

统计单词个数

Tags: /超级猛料/String.字符串处理/   Date Created:

function WordCount ( CText : string ): Longint ;

  function Seps ( As_Arg : Char ): Boolean ;

  begin

   Seps := As_Arg in

      [ #0 .. # $1F , ' ' , '.' , ',' , '?' , ':' , ';' , '(' , ')' , '/' , '\' ];

  end ;

var

 Ix                 : Word ;

 Work_Count         : Longint ;

begin

 Work_Count := 0 ;

 Ix := 1 ;

  while Ix <= Length ( CText ) do

  begin

    while ( Ix <= Length ( CText )) and ( Seps ( CText [ Ix ])) do

     Inc ( Ix );

    if Ix <= Length ( CText ) then

    begin

     Inc ( Work_Count );

      while ( Ix <= Length ( CText )) and ( not Seps ( CText [ Ix ])) do

       Inc ( Ix );

    end ;

  end ;

 Result := Work_Count ;

end ;