Hi ! We meet again ! Now, I would like to discuss about strings in depth. How are you doing ? Well, I'm having health problem this day and I need a rest. I am still able to write this to you. Cheer ! Topics discussed this time are :
Actually, string is an array of characters. So, suppose s is a string. s[1] is equal to the first character of s. s[2] is the second character, and so on. Look at this :
var s : string; begin s:='Hello, dear'; writeln(s); s[1]:='J'; { Replace the first character with J } s[5]:='y'; { Replace the fifth character with y } writeln(s); { Jelly, dear } writeln('The length of s is ',ord(s[0])); end.
Zeroth character is the character form of the length of that string. So, ord(s[0]) denotes the actual length of s. You may replace it with length(s) which gives the same effect.
Normally, strings would hold 80 characters in maximum. Suppose you would have a set of strings that is about 10 characters long. Declaring each as normal string would be a waste of space. Pascal provides facility to limit the string length. If you would like to have a string that has maximum limit of 10 characters, you would write :
var s : string[10];
Pascal also provides routines to manipulate string :
var s : string; e : integer; r : real; begin write('Enter a number : '); readln(s); val(s,r,e); if e<>0 then writeln('Error at position : ',e); else writeln('That was : ',r:4:3); end.
var s : string; i : integer; begin write('Input an integer : '); readln(i); str(i,s); writeln('That was : ',s); end.
str(r:4:3,s);s consists of 4 digits before the decimal point of r, and 3 digits after the decimal point. Example :
var s : string; r : real; begin write('Input a real : '); readln(r); str(r:4:3,s); writeln('That was : ',s); end.
var s1, s2 : string; begin s1:='not '; s2:='I do love you'; insert(s1,s2,6); writeln(s2); { I do not love you } end.
var s : string; begin s:='I am not responsible for that !'; delete(s,6,3); writeln(s); { I am responsible for that } end.
var s : string; begin fillchar(s,51,'='); s[0]:=chr(50); end.
Actually, those procedures or functions can be read from Pascal's help. So, refer to Borland Pascal help if you want working examples. You can even make your own string functions. If you don't understand, e-mail me.
As Borland Pascal 7.0 arrives, we know that C style string is adopted in. In C, we view strings either as an array of characters or a pointer of characters. Pointer will be discussed in second lesson. The main reason is that Pascal can not maintain strings larger than 255. Then a new type of string is employed : PChar, a pointer of character.
Pascal string consists of : one byte for its length then the contents. In PChar, just like C, we don't recognize the length. All we know is when the string stops -- that is when we encounter ASCII 0 (NULL). That null stop style makes the C style of string called NULL TERMINATED STRING.
All string manipulation routines for PChar are adopted from C. If you would like to know each one of it, you should learn C, where it came from. Nevertheless, you could even learn it in Borland Pascal help too.
The details of pointers and this type of string could be found in my second lesson of Pascal.
You could compare two strings just like numbers, too ! Suppose s1 and s2 are strings, you could do like this :
if s1 < s2 then .... (and so on ...)
That's all for this time.
Where to go ?
Back to main page
Back to Pascal Tutorial Lesson 1 contents
To the quiz
Back to Chapter 7 about arrays
To Chapter 9 about records
My page of programming link
Contact me here
By : Roby Joehanes, © 1996, 2000