Importance of Strings in Pascal

pascal string

There are different kinds of technologies, which has passed by in the field of Information technology for a long time now. Pascal has gained a lot of attention in the previous generation engineers because of various reasons. Most of the software testing companies in bangalore prefer using Pascal still as the first step of the learning process because it helps them to understand the basics in an easy way.

Strings of Pascal

In classic Pascal, the only string facilities are associated with packed arrays of char. This is another area in which a variety of local extensions have arisen. Extended Pascal includes provision for dynamic string types, and unifies them with classic Pascal strings and with characters.

String variables are declared with a maximum capacity, for instance:

  VAR  s1,s2: string(20);

 fname: PACKED ARRAY [1..20] OF char;

String values have a length (number of characters). A dynamic string variable such as s1 can hold a value of any length from zero up to its capacity, and the object code keeps track of the current length. With a fixed string such as fname, as found in classic Pascal, the length of the contents is equal to the capacity; when a shorter value is assigned to fname, it is padded on the right with spaces until it fits. A variable of type char has a capacity of 1. Variables of these three kinds, together with string literals and character constants, produce general string values. In addition, individual characters or substrings of string variables can be referenced by indexing, for instance s1[i] or fname[1..8].

String values can be concatenated using the + operator, and constants can be defined by constant expressions of string type, eg. ‘ABC’+chr(13). There are predeclared functions for the commonly-required string operations such as locating a substring within a longer string.

Strings can be written to or read from textfiles, and versions of the textfile read and write procedures are provided which take a string variable in place of the file, making all the conversion and editing processes available internally.

A string may be declared with capacity fixed at compile time, as in the example above, or defined by a run-time variable expression. There are also provisions for formal parameters which adjust themselves to the actual parameter at each call.

    PROCEDURE p (VAR s: string)

Dynamic strings of different capacities may be passed to this procedure with each call; the code within the procedure can discover the capacity of each actual parameter by reference to s.capacity.

If a variable n has the value 10, a string declared as string(n) has the same type as one declared as string(10) for compatibility purposes (though the type checking cannot be performed until run-time). As will be seen in the next section, this rule and the adaptable formal parameters are both particular cases of facilities that apply to all schematic types, and arise from string being formally defined to be a predeclared “schema” with additional special properties.

 


Related Post


1 Comment

Leave a Reply

Your email address will not be published. Required fields are marked *