STRINGS & STRING PROCEDURES
Example program ------> STRARRAY.PAS
According to the Pascal definition, a string is simply an array of 2 of more elements of type char, and is contained in an array defined in a var declaration as a fixed length. Examine the example program named STRARRAY.PAS. Notice that the strings are defined as types in the type declaration even though they could have been completely defined in the var part of the declaration. This is to begin getting you used to seeing the type declaration. The strings defined here are nothing more than arrays with char type variables.
A STRING IS AN ARRAY OF CHAR
The interesting part of this file is the executable program. Notice that when the variable First_Name is assigned a value, the value assigned to it must contain exactly 10 characters or the compiler will generate an error. If you edit out a blank in line 15 of yhis program, you will get an invalid type error. Pascal is neat in allowing you to write out the values in the string array without specifically writing each character in a loop. The Writeln statement in line 18 writes each of the characters in the array without us writing a loop to do so. To combine the data, called concatenation, requires the use of the rather extensive looping and subscripting seen in the last part of the program. It would be even messier if we were to consider variable length fields which is nearly always the case in a real program. However, after the fields are concatenated together, a single statement in line 26 outputs the entire string.
Two things should be observed in this program. First, notice the fact that the string operations are truly array operations and will follow all of the characteristics discussed in the last chapter. Secondly, it is very obvious that Pascal is rather weak when it comes to its handling of text type data. Pascal will handle text data, even though it may be difficult to do so using the standard description of Pascal as illustrated in this program. We will see next that TURBO Pascal really shines when it is desired to manipulate text. Compile and run STRARRAY.PAS and observe the output.
THE TURBO PASCAL STRING TYPE
Example program ------> STRINGS.PAS
Examine the example program STRINGS.PAS. You will see a much more concise program that actually does more than the last example program. TURBO Pascal has, as an extension to standard Pascal, the string type of variable. It is used as shown, and the number in the square brackets in the var declaration is the maximum length of the string. In actual use, the variable can be used as any length from zero characters up to the maximum given in the declaration. The variable First_Name, for example, actually has 11 locations of storage for its data. The current length is stored in First_Name and the data is stored in First_Name through First_Name. All data are stored as byte variables, including the size, so the length is limited to a maximum of 255 characters.
STRINGS HAVE VARIABLE LENGTHS
Now look at the program itself. Even though the variable First_Name is defined as 10 characters long, it is perfectly legal to assign it a 4 character constant, with First_Name automatically set to 4 by the system and the last six characters undefined and unneeded. When the program is run, the three variables are printed out all squeezed together indicating that the variables are indeed shorter than their full size as defined in the var declaration.
Using the string type is even easier when you desire to combine several fields into one as can be seen in the assignment to Full_Name. The concatenation operator is the plus sign and is used to combine strings and individual characters as indicated in line 14. Notice that there are even two blanks, in the form of constant fields, inserted between the component parts of the full name. When it is written out, the full name is formatted neatly and is easy to read. Compile and run STRINGS.PAS and observe the output.
WHAT'S IN A STRING TYPE VARIABLE?
Example program ------> WHATSTRG.PAS
The next example program named WHATSTRG.PAS, is intended to show you exactly what is in a string variable. This program is identical to the last program except for some added statements at the end. Notice the assignment to Total. The function Length is available in TURBO Pascal to return the current length of any string type variable. Note that it returns the current length, not the defined maximum size of the string. It returns a byte type variable with the value contained in the  position of the variable. We print out the number of characters in the string at this point, and then print out each character on a line by itself to illustrate that the TURBO Pascal string type variable is simply an array variable.
The TURBO Pascal reference manual has a full description of several more procedures and functions for use with strings which are available in TURBO Pascal only. Refer to your TURBO Pascal reference manual for complete details. The use of these should be clear after you grasp the material covered here.
Advance to Chapter 8
Return to the Table of Contents