home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Learn Pascal in a Three Days (2nd Ed.)
(Publisher: Wordware Publishing, Inc.)
Author(s):
ISBN: 1556225679
Publication Date: 07/01/97

Bookmark It

Search this book:
 
Previous Table of Contents Next


6-5 Reading a File of Text: EOF

Another boolean function EOF is used to detect the end-of-file mark. The function is FALSE until the end-of-file mark is reached, at which time it becomes TRUE. When using the keyboard for input, the end of file is reached if you press Ctrl+Z (ASCII 26). This function is useful for reading several lines of text (a file). You can use EOF along with EOLN to read and analyze several lines of text as follows:

    WHILE NOT EOF DO
     BEGIN
      WHILE NOT EOLN DO
       BEGIN
        READ(Ch);
        ...                { Processing data }
       END;                { End of line }
      READLN               { Advance the pointer }
     END;                         { End of file }

In this code, the file is read line by line. After a complete line has been read, the EOLN function becomes TRUE and no more characters are read from this line. The READLN statement is then used to advance the pointer to the beginning of the next line. The program ends when the end-of-file mark is detected and the outer loop is terminated. Let us see an example.

Example: Frequency Counter

The following program asks you to enter a letter. Then it starts reading whatever you type from the keyboard. When you press Ctrl+Z the program ends and displays how many times the specified letter was repeated in the file.

{ -------------------- figure 6-8 -------------------- }
PROGRAM FreqCounter1(INPUT,OUTPUT);
VAR
 Ch, SpecificChar   :CHAR;
 Counter, FreqCounter :INTEGER;
BEGIN
 Counter:= 0;
 FreqCounter:= 0;
 WRITE('Enter the required letter: ');
 READLN(SpecificChar);
 WRITELN('Start typing. Press Ctrl+Z to finish.');
 WHILE NOT EOF DO
  BEGIN
   WHILE NOT EOLN DO
    BEGIN
     READ(Ch);
     IF (Ch > = 'A') AND (Ch < = 'Z') OR
          (Ch > = 'a') AND (Ch < = 'z') THEN
        Counter:= Counter + 1;
     IF Ch = SpecificChar THEN
        Freqcounter:= FreqCounter + 1;
    END;
   READLN
  END;
 WRITELN('Total number of letters= ', Counter);
 WRITELN('The letter ''', SpecificChar, ''' was repeated ',
      FreqCounter, ' time(s)');
 WRITELN('Frequency of repetition= ', freqCounter/Counter*100:2:2,'%')
END.

The specific letter is assigned to the variable “SpecificChar” and compared to the input letter “Ch.” If the comparison is TRUE, the “FreqCounter” is incremented by one. The total number of letters is accumulated in the variable “Counter.” The frequency of repetition of the letter is calculated by dividing “FreqCounter” by “Counter” and multiplying the result by 100.

Sample run:

Enter the required character: a
Start typing. Press Ctrl-Z to finish.
This is a test to count the repetition frequency
of the letter "a" in a keyboard file
^Z

Total number of letters= 67
The letter 'a' was repeated 4 time(s)
Frequency of repetition= 5.97%

6-6 String Manipulation

In Chapter 2 you learned how to declare, read, and write variables of the type STRING, which was introduced by the modern Pascal implementations (such as Turbo, UCSD, and Mackintosh). You also learned how to use the function LENGTH to count the number of letters in a string. In this section you are introduced to more string features that help in manipulating text.

Tips on String Input/Output

For both input and output, you may either treat a string variable as one unit, or you may treat it as an array whose elements are the characters that make up the string. Look at this simple program, which reads a string variable and displays it character by character, with each character on a separate line (using the LF character).

{ -------------------- figure 6-9 -------------------- }
PROGRAM String1(INPUT,OUTPUT);
CONST
 LF = CHR(10);
VAR
 Name:STRING[30];
 I :INTEGER;
BEGIN
 WRITE('Please enter a name: ');
 READLN(Name);
 FOR I:= 1 TO LENGTH(Name) DO
  WRITE(Name[I],LF)
END.

Sample run:

Please enter a name: PASCAL
P
 A
  S
   C
    A
     L

Example: Sorting Names

You may build an array of the type STRING to store related items such as names or addresses. In this way, you can sort names in alphabetical order using the same algorithm which you have used before to sort numbers. Each two strings are compared character by character. So, the following expressions are TRUE:

    'Able' < 'Baker'
    'Baker' < 'Charlie'
    'Charley' < 'Charlie'

All uppercase letters are greater than lowercase letters. Also, the leading and trailing spaces are included in the comparison. The ASCII code of the blank space (32) is less than that of any letter or digit. In the following program an array of four names is read, sorted, and displayed.

{ -------------------- figure 6-10 ------------------- }
PROGRAM SortStrings(INPUT,OUTPUT);
CONST
 Tab = ' ';
 NumOfElements = 4;
TYPE
 StringArray = ARRAY[1..NumOfElements] OF STRING[30];
VAR
 Name     :StringArray;
 I, J :INTEGER;
 Temp     :STRING[30];
BEGIN
{ Read the array elements }
{ ---------------- }
  FOR I:= 1 TO NumOfElements DO
   BEGIN
     WRITE('Please enter name #', I, ': ');
     READLN(Name[I])
   END;
{ Sort names }
{ ------- }
  FOR I:= 1 TO NumOfElements-1 DO1
   FOR J:= I+1 TO NumOfElements DO
    IF Name[I] > Name[J] THEN
       BEGIN
        Temp:= Name[I];
        Name[I]:= Name[J];
        Name[J]:= Temp
       END;
    { End of inner and outer loops }
{ Display sorted names }
{ --------------- }
  WRITELN('Serial # Name');
  WRITELN('-------------------');
  FOR I:= 1 TO NumOfElements DO
   WRITELN(I:2, Tab, Name[I])
END.

Sample run:

Please enter name #1: Rigby, Peter
Please enter name #2: Berlin, Amy
Please enter name #3: Sanders, Dale
Please enter name #4: Brady, Clark

Serial # Name
-------------------
1     Berlin, Amy
2     Brady, Clark
3     Rigby, Peter
4     Sanders, Dale


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.