Home Random Page


CATEGORIES:

BiologyChemistryConstructionCultureEcologyEconomyElectronicsFinanceGeographyHistoryInformaticsLawMathematicsMechanicsMedicineOtherPedagogyPhilosophyPhysicsPolicyPsychologySociologySportTourism






More on the type STRING

With this program we move on to using the Pascal type STRING. This as previously discussed can be thought of as a list of any of the ASCII characters, and so can be letters and digits, but any numbers held in the type STRING cannot be used in calculations.

Program 9

PROGRAM customer(INPUT,OUTPUT); { a program which manipulates strings to build names and addresses } VAR forename, surname, road, town : STRING[15]; number : STRING[4]; customer_name : STRING[31]; { 15 + 1 + 15 is max size } customer_address : STRING[50]; BEGIN WRITE('Please enter the customer''s first name : '); READLN(forename); WRITELN; WRITE('Please enter the customer''s last name : '); READLN(surname); WRITELN; WRITE('Please enter the customer''s address'); WRITE('Flat/House number : '); READLN(number); WRITE('Street/Road, etc : '); READLN(road); WRITE('Town/City, etc ? : '); READLN(town); customer_name := forename + ' ' + surname; customer_address := number + ' ' + road + ', ' + town + '.'; WRITE('The customer ',customer_name,' lives at'); WRITELN(customer_address); END. { program customer }


What's New? For the first time we are using the type STRING in Pascal. All the VAR declarations are strings. In the text of the first three input lines, the double apostrophes are required in Pascal, because it takes a single one to mean end of text. If you want to use apostrophes in your written output you must remember to do this. You will get a compiler generated syntax error if you forget.

Appending Strings


In the two lines:

customer_name := forename + ' ' + surname; customer_address := number + ' ' + road + ', ' + town + '.';


The apostrophes in the middle are only there to add a space between the variables name and surname. This is the space character, but any character could be put in its place such as in the second line with the comma and the full stop. The addition sign is the append/concatenate operator for strings.

Inspecting Strings

When a string is declared, the simplest way to think of this is as consecutive cells being set aside in memory. As with the type INTEGER and REAL we can similarly think of the type STRING as a box with a number of compartments.
The declaration of a STRING follows the form:

VAR name : STRING[10];


There are ten compartments in the box called name. Each of these compartments is numbered according to the VAR declaration, in the case of name from 1 to 10. Each compartment, therefore can be inspected, by using one of these numbers.


Figure 2 - Strings As Compartmentalised Boxes


Let us assume that the piece of code:

name := 'Phil Grant';


has assigned this string to the VAR name. In order to write out the contents of the second compartment in the box name, requires the code:

WRITELN(name[2]);


This is all the code needed to output the second character of the string name.
Note:- That name[5] actually contains the ASCII character <space>, not nothing as it might appear. The numbers used this way are called subscripts.



Program 10

PROGRAM name_1(OUTPUT); { string demonstration program } VAR name : STRING[10]; BEGIN name := 'Phil Grant'; WRITELN; WRITELN('The second character of ',name,' is ',name[2]); END. { program name_1 }


An interesting alternative to this program is to use, as a subscript , a variable of type INTEGER, which is then used to access an element in a string. This allows the user to input which compartment number they wanted to inspect. It is usual for these subscripts to be named using capital letters in the range I, J, K, L. This is just programming convention, and is not necessary, but is helpful to readers who look for such conventions to aid their understanding of a program. The alternative program looks like this.

Program 11

PROGRAM name_2(INPUT,OUTPUT); VAR a_name : STRING[10]; I : INTEGER; BEGIN WRITE('Please type in a name : '); READLN(a_name); WRITELN; WRITE('Enter a number between 1 and 15 : '); READLN(I); WRITE('The "',I:2,'th" character in "'); WRITELN(a_name,'" is "',a_name[I],'"'); END. { program name_2 }


What's New? I think that after five different programs, you should be able to figure out what's going on with this one for yourself. But I will emphasise the power, that the use of subscripts gives. We will come back again to subscripts in a different context later. It is very important that you realise, and try to think of the type STRING as nothing more than short hand for a collection of any ASCII characters. You will be tempted, because of the way that the type STRING is used in most of your own, and other peoples' programs to think of a string as a data type for storing only words. Remembering, as I said earlier that any ASCII character can be put into a string. This means that legal strings include:

  • 'Colin & Sandra Arnold'
  • '3ab*//'
  • '12345-1234'
  • '3.141592654'


The single apostrophe at each end is quite important. You must use this method to assign a string to a string variable, as I have done with the_name below. This is no different from actually writing it out using a WRITELN, if you think about it.

WRITELN('Alan MacDonald'); the_name := 'Alan MacDonald'; WRITELN(the_name);


This means that the type STRING is a very flexible data type. A case in point is when a friend of mine was writing a Pascal program in his job as an engineer which needed a variable which would accept a ten digit part number. He soon discovered that his version of Pascal did not have a large enough MAXINT for the job. This is fairly common with P.C. based Pascal. So he found that the program would not run correctly, so he changed the type of his part number variable to REAL from INTEGER, now his program could run. I suggested that he could use

VAR part_number : STRING[10];


I demonstrated that these variables still could be compared to each other with the comparison operators <, >, and =. He realised how this would be a much better solution to his problem and changed his program. This also prevented the part number '0012345678' being changed to '0.12345679000E+07' which is what Pascal does with REAL numbers. As you can see this is not a very good solution to having a ten digit variable in your programs. In this real work place example, part numbers are usually coded. That is, that some of the numbers have a meaning. That could mean that the first three elements of the part number might tell you what kind of component the part is. For instance, the first three might be 014, which would be a printed circuit board. Then 021, could be a computer chip. With this system string inspection can be performed on the part number.

IF (part_number[1] = 0) AND (part_number[2] = 1) AND (part_number[3] = 4) THEN BEGIN WRITELN('The part is a printed circuit board'); WRITELN(', and is located is store room section A1'); END;


This is not necessarily a very realistic example, but I am sure that you will get the point. You should hold off going on to the next chapters until you have written a few programs from scratch for yourself. You can tinker with these ones, or try to come up with ones of your own.


Date: 2016-03-03; view: 713


<== previous page | next page ==>
Field Width Specifiers | Reading And Writing Variables
doclecture.net - lectures - 2014-2024 year. Copyright infringement or personal data (0.009 sec.)