Pascal Programming/Strings

< Pascal Programming

String is just an array of ASCII characters.

Definition

string type is defined like this:

 type string = packed array [0..255] of char;

Consider this code:

 program string_sample;
 uses crt;
 var s: string;
     i: longint;
 begin
  s:="This is a example of string";
  writeln(s[1]);
  writeln(ord(s[0]));
  readln;
 end.

The output:

T
27

We can see that:

Declare

Just like longint or integer:

 var s: string;

In the above example, string s can store up to 255 characters.

But what if you want the string s to store up to 10 characters?

 var s: string[10]; {OK, so now s can only store up to 10 chars}
This article is issued from Wikibooks. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.