Link to home
Start Free TrialLog in
Avatar of zattz
zattz

asked on

Count number of words in a text file

Hi,

I'm looking for some code that will open a text file and count the number of words in it.

Thanks
ASKER CERTIFIED SOLUTION
Avatar of MerijnB
MerijnB
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of TName
TName

The same as a function:

procedure TForm1.Button1Click(Sender: TObject);  
begin
   ShowMessage(IntToStr(CountFileWords('C:\Test.txt')));
end;


function CountFileWords(FName:String):Integer;
var
SL:TStringList;
i,j:Integer;
s:String;
begin
   Result:=0;
   SL:=TStringList.create;
   try
     SL.LoadFromFile('C:\Test.txt');
     for i:=0 to SL.Count-1 do begin
       s:=Trim(SL[i]);
       if Length(s)>0 then
         for j:=1 to Length(s) do
           if (((s[j]<>' ') and (j=1)) or ((s[j]<>' ')and(s[j-1]=' '))) then
             inc(Result);
   end;
   finally
     SL.Free;
   end;
end;
Oops, in the case of the function it should of course be:

  SL.LoadFromFile(FName);

instead of

 SL.LoadFromFile('C:\Test.txt');
To exclude non-word stand alone chars, the function could be expanded like this
(any char can be added to "NotValid")

function CountFileWords(FName:String):Integer;
var
SL:TStringList;
i,j:Integer;
s:String;
var NotValid : set of Char;
begin
   Result:=0;
   NotValid:= ['.',',','-','_','|','<','>'];
   SL:=TStringList.create;
   try
     SL.LoadFromFile(FName);
     for i:=0 to SL.Count-1 do begin
       s:=Trim(SL[i]);
       if Length(s)>0 then
         for j:=1 to Length(s) do
           if (((s[j]<>' ') and (j=1)) or ((s[j]<>' ') and ((s[j-1]=' ') or (s[j-1] in NotValid)))
               and (not(s[j] in NotValid))) then
             inc(Result);
     end;
   finally
     SL.Free;
   end;
end;

An exact definition of what a word is can help decide which method is best (e.g use ['a'..'z','A'..'Z']...)