
The Turbo course's S10 gave you string[20], Length, Copy, Pos and +. HiSoft Pascal has none of them. There is no string type at all. Text lives in the plainest thing Pascal has for it - an array of characters - and anything more you write yourself.
A word of up to eight letters is:
TYPE WORD=ARRAY[1..8] OF CHAR;
VAR W:WORD;
It behaves much as a Turbo string did in the easy cases. WRITELN(W) writes the whole array. W[1] is its first character, and can be changed. Two of them compare with = and <, character by character. And a quoted text can be put into it -
W:='EINSTEIN';
W:='HI' is error 10: two characters will not go into eight. Pad it with spaces yourself: W:='HI '. An empty text, '', is not allowed at all.Name the type, as above, and declare everything with it: HiSoft Pascal will only let one array be given to another if both were declared with the same type (S11).
READ(W) with an array of characters takes a word from the keyboard. If the word is shorter than the array, the rest is filled with CHR(0) - the character with code 0 - and WRITELN(W) then writes only the letters, because CHR(0) prints as nothing. If it is longer, only the first eight letters are kept.
As always with the keyboard in HiSoft Pascal, a READLN first (S9).
With no Length, you count: the letters up to the first CHR(0), or all eight if there is none. The obvious loop is:
WHILE (N<8) AND (S[N+1]<>CHR(0)) DO N:=N+1;
and in HiSoft Pascal it is wrong. AND always works out both sides. When N reaches 8, N<8 is false - but S[9] is looked at anyway, and there is no S[9]: the program stops with Index too High. Write the test so that it never looks past the end - the listing below does it with REPEAT and a Boolean.
10 PROGRAM WORDS;
20 TYPE WORD=ARRAY[1..8] OF CHAR;
30 VAR W:WORD;
40 FUNCTION LEN(VAR S:WORD):INTEGER;
50 VAR N:INTEGER; DONE:BOOLEAN;
60 BEGIN
70 N:=0; DONE:=FALSE;
80 REPEAT
90 IF S[N+1]=CHR(0) THEN DONE:=TRUE
100 ELSE N:=N+1
110 UNTIL DONE OR (N=8);
120 LEN:=N
130 END;
140 BEGIN
150 WRITE('WORD',CHR(63),' ');
160 READLN;
170 READ(W);
180 WRITELN('[',W,'] ',LEN(W),'LETTERS');
190 IF W='EINSTEIN' THEN WRITELN('HELLO, EINSTEIN')
200 END.
Line 150's CHR(63) is the question mark, which cannot be typed into XBAS (S3). Line 180 needs no space before LETTERS: the number brings its own.
Starting from
WORDS.ASC saved from XBAS; HPEIN WORDS.
What you should see
Run it three times, typing HI, then EINSTEIN, then TATUNGEINSTEIN:
WORD? HI
[HI] 2 LETTERS
WORD? EINSTEIN
[EINSTEIN] 8 LETTERS
HELLO, EINSTEIN
WORD? TATUNGEINSTEIN
[TATUNGEI] 8 LETTERS
190 W:='HI'. Will it compile?190 W:='HI '; WRITELN('[',W,'] ',LEN(W),'LETTERS') - HI and six spaces. What does LEN say, and why is it not 2?WHILE ... AND ... loop from above in place of lines 70 to 110 (with N:=0; before it). Which of the three words still work?8.1 Write a procedure that writes a WORD backwards, leaving out any CHR(0)s.
8.2 Write a function SAME(A,B:WORD):BOOLEAN that is TRUE when the two words have the same letters, ignoring the difference between capitals and small letters. (A small letter's code is 32 more than its capital's.)
Worked solutions are in Appendix II.
| Symptom | Cause |
|---|---|
*ERROR* 3 at STRING | There is no string type. Use ARRAY[1..n] OF CHAR. |
*ERROR* 10 at a quoted text | It is not exactly the array's length. Pad it with spaces. |
*ERROR* 33 | An empty text, ''. Not allowed. |
Index too High in a loop over a word | An AND looked past the end - both sides are always worked out. |
| A word typed has lost its end | It was longer than the array. |
There is no string: text is an ARRAY[1..n] OF CHAR, given quoted texts of exactly n characters. READ fills a shorter word out with CHR(0), which prints as nothing. Length, copying and searching you write yourself - remembering that AND always works out both sides.
S9, Asking A Question - READ and READLN at the keyboard, and the empty line every program starts with.