PROGRAM TWENTY-ONE
Write a program to count the number of characters in a text file. The
valid characters are 'A' to 'Z', and 'a' to 'z'.
program PROG21 (input,output, infile); {count characters in file}
type legal1 = 'A'..'Z';
legal2 = 'a'..'z';
var infile : TEXT;
fname : string[15];
ch : char;
count : integer;
begin
count := 0;
writeln('Please enter name of text file to count.');
readln( fname );
{ for turbo pascal
assign( infile, fname );
reset( infile );
}
reset( infile, fname ); {open a file using filename stored in}
{array fname }
while not eof( infile ) do
begin
while not eoln( infile ) do
begin
read( infile, ch );
if ((ch>='A')and(ch<='Z'))or((ch >='a')and(ch<='z')) then
count := count + 1
end;
readln( infile ) {read eof character}
end;
close( infile ); {close filename specified by fname}
writeln('The number of characters in ',fname,' is ',count)
end.
Copyright B Brown/P Henry/CIT, 1988-1997. All rights reserved.