home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
CP/M
/
CPM_CDROM.iso
/
cpm
/
turbopas
/
ttutor2.lbr
/
TL13.TQT
/
TL13.TXT
Wrap
Text File
|
1985-08-02
|
5KB
|
227 lines
TURBO-LESSONS - A Pascal Tutorial Version 1.01 Page 60
TURBO-LESSON 13: STRINGS
OBJECTIVES - In this lesson, you will learn about:
1. Strings
2. String replacement statement
3. Predefined string function, LENGTH
1. Strings.
You have already seen some Pascal strings in the WriteLn
statements of earlier lessons.
WriteLn('This is a string.');
It is often convenient to store strings as variables or
constants.
A string constant may be defined in the CONST section:
CONST String_1 = 'TURBO-LESSONS';
String variables must be declared in the VAR section. The form
of the declaration is:
VAR First_Name : String[12];
This sets up storage for a variable named First_Name which can
store a string up to 12 characters long.
2. String replacement statement.
The replacement statement for strings is:
String_Name := (string expression);
##### DO:
Examine PROG13. Notice the following:
A string constant, S_Test is given the value 'Test String' in the
CONST declaration section.
Several string variables are defined in the VAR section.
î
TURBO-LESSONS - A Pascal Tutorial Version 1.01 Page 61
##### DO:
Run the program.
What happens when an attempt is made to store too long a string
in a string variable?
##### DO:
Add the following to the program:
S5 := S_Test;
WriteLn(S5);
Run the program.
How many characters of S5 are printed?
##### DO:
Modify WriteLn(S5) to:
WriteLn('[', S5, ']');
Run the program. How many characters of S5 are printed?
DEBUGGING NOTE: When working with strings, you may find it
helpful to print some kind of marker before and after a string to
help "see" the occurrences of the character, blank.
You have seen what happens when storing 'Test String' in too
short a variable: S3 holds 'Tes', S8 holds 'Test Str'.
What happens when a string is stored in a variable that is larger
than needed? Are blanks added?
##### DO:
Modify the WriteLn(S14) to bracket S14 (like you did above with
S5) and run the program.
How many characters of S14 were printed?
Were extra blanks added? (More on this later.)
î
TURBO-LESSONS - A Pascal Tutorial Version 1.01 Page 62
##### DO:
Look at PROG13A.
WriteLn(S8[I]);
Notice the use of the square brackets in the statement above.
This is a way to refer to a specific character in a string.
S8[2] means the 2nd character in the string, S8.
NOTE: SQUARE BRACKETS ARE USED IN TWO DIFFERENT WAYS WITH
STRINGS. WHEN DECLARING VARIABLES, THE BRACKETS ENCLOSE THE
MAXIMUM LENGTH OF THE STRING. IN PROCESSING STATEMENTS, THE
NUMBER IN THE BRACKETS DESIGNATE A PARTICULAR CHARACTER IN A
STRING.
##### DO:
Run the program, using 2 as position number.
Try 5 as an input. What character was stored as the 5th
character of S8?
The string stored in S8, 'TURBO', is 5 characters long
but S8 is 8 characters long.
What characters, if any, are stored in S8[6], S8[7], and
S8[8]?
##### DO:
Run the program with input values of 6, 7, and 8.
What characters were printed?
##### DO:
Add the following statement as the first statement in the BEGIN
END block:
S8 := '12345678';
Run the program again, using 6, 7, and 8 as input.
What do you conclude about "unused" positions in a string?
Before you are prompted to enter the "position number", S8 is
printed.
Do positions 6, 7, 8 of the string print? Why?
î
TURBO-LESSONS - A Pascal Tutorial Version 1.01 Page 63
##### DO:
Run the program once more, this time use 0 as the input value.
What character printed?
If you look up this character, clubs symbol, in the ASCII chart
in your BASIC manual, you find that it is the character
associated with ASCII value 8.
String variables in TURBO are one character longer than the
maximum length you specify. This extra character is at the
beginning of the string, at position 0, and always contains the
length of the string stored.
So why isn't the length stored as a number?
Storing the length information as a character makes position 0
the same type as the other characters in the string.
3. Predefined string function, LENGTH.
Because the length of a string is often needed in processing,
the function, LENGTH, has been provided for that purpose.
##### DO:
Add the following statement after the UNTIL statement:
WriteLn('Length of string: ', LENGTH(S8) );
Run the program.
There is also another way to get the ASCII value of the character
at position 0 of a string.
##### DO:
Add the following statement just before the END:
WriteLn('ASCII value: ', ORD(S8[0]) );
Run the program.
Does the ASCII value agree with the value obtained with the
LENGTH function?
î
TURBO-LESSONS - A Pascal Tutorial Version 1.01 Page 64
##### DO:
Just before the END, insert:
FOR I := 0 to LENGTH(S8) DO
WriteLn('Position ', I:2, ': ',S8[I]);
Run the program.
##### DO:
Change the CONST declaration to:
S_Test = 'OK';
Run the program.
Also try 'Wake Up' for S_Test.
î