Open In App

What are Pascal Strings ?

Last Updated : 12 Jul, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

This article intends to provide a basic understanding of traditionally used strings i.e. “Pascal Strings”. As the name is self-explanatory, pascal strings are an essential constituent of the PASCAL programming language. Pascal is a computer programming language that Niklaus Wirth of Switzerland created around 1970 to teach organized programming. 

What are Pascal Strings?

Pascal string is a sequence of characters with optional size specification. It may contain numeric characters, letters, blanks, special characters or a combination of all of them.

Note: Extended Pascal provides several types of string objects that depends on the implementation and the system.

  • Pascal Strings was the first and foremost practice of pre-defining the length of the string to be used in the code in the pascal programming language. 
  • This string followed the same array size initialization property, i.e. it can hold data less than the defined size but not more than it as memory is being statically allocated. 

Refer to the attached screenshot for a better understanding:

Pascal String

Pascal String

  • Here in the given code address is a pascal string with a predefined size of 60. And here, the capacity of the string address has been mapped in the variable “houseAndStreet”. Thus, we can state that the length of houseAndStreet variable can be less than 60 but not more than that.

Types of PASCAL Strings:

  1. Character arrays: They consist of letters, numbers, special symbols, etc. Pascal programming language may accommodate up to 256 characters. A series of zero or more bytes-sized characters wrapped in a single quote make up this character string.
  2. String variables: The variable of String type, in PASCAL programming. These variables are declared inside the var block as per the PASCAL programming syntax. Refer to line 5 in Fig 1.
  3. Short strings: This string type is used whenever the total capacity remains constant throughout, here the size is specified on a prior basis. The code won’t break even if we give an input larger than the variable’s declared size but while retrieving the output of the same variable, it will ignore the extra characters above the declared size.
  4. Null terminated strings: It is a typical null-terminated string that has the exact same structural layout as a C string. In reality, the main reason for the creation of this type was to provide interoperability with C class libraries.
  5. AnsiStrings: Long strings are another name for AnsiStrings. The maximum size for an AnsiStrings on 32-bit systems is 2GB. These types of strings came into existence to overcome the capacity factor. 

Some important Pascal String Functions:

S. No.     Function                                                                                                         Significance               
1.  function AnsiCompareStr(const S1: ; const S2:):Integer; This function is used for comparing two different Strings. 
This function returns an integer value.
2. function AnsiCompareText(const S1: ; const S2:):Integer; This function is used for comparing two different Strings. 
This function returns an integer value, but here the inputs are case sensitive.
3. function AnsiLowerCase(const s:): This function is used for converting the strings to all-lowercase.
4. function AnsiLastChar(const S:):PChar; This function returns the last character of the string.
5. procedure AppendStr(var Dest: ; const S:); This function appends two different strings. Here, S is appended to the end of Dest.

These were the few PASCAL string functions. 

Advantages of Pascal Strings:

  • The first bit of the pascal strings tells us the size or capacity of the variable. This reduces the time complexity of finding the size of the string to 0(1).
  • The cases where the required length is close to the assigned capacity it turns out to save a lot of memory as a whole.
  • Memory allocation is not an expensive and complex procedure in pascals strings, as it is being allocated statically not dynamically.
  • Easy to understand the code and initialization syntax as it is very close to the standard native language of English.

Disadvantages of Pascal Strings:

  • Predefining the size of the string is not useful in the cases where the upcoming requirement has no clear range in terms of length to be defined.
  • The predefined length of the string cannot be overwritten during the runtime as this cause a conflict in the code and will lead to code failure and will terminate the program finally.
  • While defining a new pascal string, the same block of code will be repeated n times this will increase the lines of the code.

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads