If this text is stored in a file, you can read it in either line per line or character per character (if you read lines, you have to loop through the line to get each character). What you also need is an array with 256 entries of, say word or longint (this depends on the size of the text). Each entry in the array represents a counter for a certain character (e.g. entry 65 represents the character 'A').
For each character you read, you have to increase the appropriate counter. If you don't want to count
characters but
letters, you should convert all read characters to uppercase so that 'A' = 'a'.
A general outline of the program would be as follows:
Code:
VAR counters : array[#0..#255] of longint;
repeat
ch:=getcharacter(f);
inc(counters[ch]);
until eof(f);
I used the
function here to indicate that the next character should be provided, this can be either directly from file of from a string (when the file is read line by line). Note, however, that reading the file line by line adds a little to the complicatedness of the code, but will in theory be faster.
The reason why I declared the counters array with character indices in stead of integer indices is that, like this, it's easier to address it. If I would have used integers (
) then conversion would be needed from character to byte in the count loop. Note that using characters as indices in an array is perfectly valid Turbo Pascal code.
Another remark: if you read the file line by line, you will never encounter a CRLF (carriage return/line feed), when reading in characters you do count these character combinations.
And finally, you can read faq935-3446 to check out how to use text files.
Regards,
Bert Vingerhoets
vingerhoetsbert@hotmail.com
Don't worry what people think about you. They're too busy wondering what you think about them.