Chapter 7 STRINGS & STRING PROCEDURES PASCAL STRINGS ____________________________________________________________ According to the Pascal definition, a ================ string is simply an array of 2 of more STRARRAY.PAS elements of type char, and is contained in ================ an array defined in a var declaration as a fixed length. Look at the example program STRARRAY.PAS. Notice that the strings are defined in the type declaration even though they could have been 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. Try editing out a blank and 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 as can be seen in the Writeln statement. 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. 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 here. Compile and run STRARRAY.PAS and observe the output. THE TURBO PASCAL STRING TYPE ____________________________________________________________ Look at the example program STRINGS.PAS. You will see a much more concise program that actually does more. TURBO Pascal has, as an extension to standard Pascal, the string type of Page 7-1 Strings and String Procedures variable. It is used as shown, and the ================= number in the square brackets in the var STRINGS.PAS declaration is the maximum length of the ================= string. In actual use in the program, 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[0] and the data is stored in First_Name[1] through First_Name[10]. All data are stored as byte variables, including the size, so the length is therefore 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[0] 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. 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? ____________________________________________________________ The next example program named ================ WHATSTRG.PAS, is intended to show you WHATSTRG.PAS 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. It returns a byte type variable with the value contained in the [0] 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. Page 7-2 Strings and String Procedures 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 version 3.0 reference manual in chapter 9, beginning on page 67, or if you are using TURBO Pascal version 4.0, you will find the string functions throughout chapter 27. The TURBO Pascal Reference Guide for version 5.X has a list of string procedures and functions on page 120. The use of these should be clear after you grasp the material covered here. PROGRAMMING EXERCISE ____________________________________________________________ 1. Write a program in which you store your first, middle, and last names as variables, then display them one to a line. Concatenate the names with blanks between them and display your full name as a single variable. Page 7-3