
Numbers are half of what programs handle. The other half is text: names, messages, the name of a level in a game. Pascal keeps text in strings, and Turbo gives you a handful of tools for measuring them, cutting them up and putting them together.
A string variable is declared with the word string and, in square brackets, the most characters it may ever hold:
var
First, Last, Full: string[30];
Remember that the Einstein draws [ and ] as arrows (S3). They are typed with the LEFT and RIGHT arrow keys.
The number is required - Turbo has no string without a length, and S: string; stops with Error 8: '[' expected - and it may be anything from 1 to 255. Choose it to suit the longest text the variable will ever hold.
You give a string a value with :=, as with a number, and the text goes in single quotes:
First := 'Albert';
If the text is longer than the string can hold, the end is cut off, with no warning. Put 'Tatung Einstein' into a string[5] and you have 'Tatun'.
And '' - two quotes with nothing between - is the empty string, with no characters at all.
To put a quote mark inside a string, write it twice: 'Don''t' prints Don't.
+ joins strings end to end:
Full := First + ' ' + Last;
Length(S) is how many characters S holds now - not how many it could hold. With Full holding Albert Einstein, Length(Full) is 15.
| Tool | Does | With T = 'Tatung Einstein' |
|---|---|---|
Copy(T, 8, 4) | 4 characters starting at the 8th | 'Eins' |
Copy(T, 8, 50) | asking for more than there is just stops at the end | 'Einstein' |
Pos('stein', T) | where 'stein' starts in T | 11 |
Pos('x', T) | 0 if it is not there | 0 |
And two that change the string in the variable itself:
| Tool | Does | Result |
|---|---|---|
Delete(T, 1, 7) | removes 7 characters starting at the 1st | T is 'Einstein' |
Insert('Mr ', T, 1) | puts 'Mr ' in at position 1 | T is 'Mr Einstein' |
Copy and Pos are functions: they give back an answer, and you use it in a sum, a Writeln or on the right of :=. Delete and Insert are procedures: they are statements on their own, and they change the variable you give them. Pascal is strict about the difference, and the difference is S15's subject.
S[i] is the i-th character of S, counting from 1. With S holding 'Einstein', S[1] is E. You can change one character the same way:
S := 'Hello';
S[1] := 'J';
and S is 'Jello'. The last character of any string is S[Length(S)].
A single character has a type of its own, Char:
var
C: Char;
...
C := S[1];
Every character has a number, its code. Ord(C) gives it: Ord('E') is 69. Chr(n) goes the other way: Chr(66) is B. And UpCase(C) gives the capital of a letter: UpCase('q') is Q.
A string that looks like a number is still a string; Turbo will not put one into an integer (Error 44: Type mismatch). Two procedures convert:
Str(N:w, S) puts the number N into the string S as text, laid out just as Writeln would lay it out with :w. Str(42:5, S) makes S hold ' 42'.Val(S, N, Code) reads the number in S into N. Code says whether it worked: 0 if it did, otherwise the position of the first character that is not part of a number. Val('123', N, Code) gives 123 and 0; Val('12x', N, Code) gives Code 3, pointing at the x.program Names;
var
First, Last, Full: string[30];
begin
First := 'Albert';
Last := 'Einstein';
Full := First + ' ' + Last;
Writeln(Full);
Writeln('Letters: ', Length(Full));
Writeln('Initials: ',
First[1], Last[1]);
Writeln('Surname at: ',
Pos(Last, Full));
end.
Starting from
Turbo freshly started, Y, a new work file called NAMES.
What you should see
Albert Einstein
Letters: 15
Initials: AE
Surname at: 8
Full on a line of its own as string[10]. What is printed now, and why does Pos give a different answer?First[1], Last[1] to First[2], Last[2]. What are the "initials" now?Writeln(Full); to Writeln(Copy(Full, 1, 3));. What is printed?10.1 Join 'Turbo' and 'Pascal' with a space between, and print the result and how many characters it has.
10.2 Put a word in a string variable, and print its first letter and its last letter.
10.3 A string holds 'Albert Einstein'. Using Pos to find the space, and Copy, print it the other way round: Einstein, Albert.
Worked solutions are in Appendix II.
| Symptom | Cause |
|---|---|
Error 8: '[' expected | string without its length in square brackets. |
Error 49: Invalid String length | A length over 255. |
| The end of your text has gone | The string variable is too short for it. Nothing warns you. |
Error 44: Type mismatch putting a string into a number | Use Val. |
Error 55: String constant exceeds line | A closing quote is missing - or you meant a quote mark inside the text and wrote only one. Write it twice. |
Pos gives 0 | The text you searched for is not there - check its capitals, and that the string was not cut short. |
string[n] holds up to n characters, cut off silently if you give it more. + joins strings; Length measures; Copy and Pos pick out and find; Delete and Insert change a string in place. S[i] is one character, a Char; Ord and Chr go between characters and codes, and UpCase makes capitals. Str and Val go between numbers and strings.
S11, Asking A Question - getting numbers and words from the person at the keyboard while the program runs.