Fillchar Procedure – Fillchar/Fillbyte And Finalize()

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 arest. I am still able to write this to you. Cheer !Topics discussed this time are :String as array of charLimited length stringString manipulation commands : Length, Copy, Pos, Val, Str, Concat, Insert, Delete, FillcharString as a pointer of characters (PChar), introductionLet”s begin !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.

Đang xem: Fillchar

Normally, strings would hold 80 characters in maximum. Suppose you wouldhave a set of strings that is about 10 characters long. Declaring each asnormal string would be a waste of space. Pascal provides facility to limitthe string length. If you would like to have a string that has maximum limitof 10 characters, you would write :

var s : string<10>;Pascal also provides routines to manipulate string :Length : return string length. Syntax : length(s) Example : n:=length(s); Suppose s:=”Who are you ?”; n would be 13.Copy : get an excerpt from a string. Syntax : copy(s,from,howmuch) Example : st:=copy(s,5,3); Get an excerpt of 3 characters from s, beginning from the 5th character. Suppose s:=”Who are you ?”; st will be “are”. Note that if index from is greater than the length of s, st would be empty, example :st:=copy(s,15,4); { empty string } If howmuch exceed the end of the string s, it returns the remainder of the string, example :st:=copy(s,9,10); st would be equal to “you ?”Pos : get the position of a substring from a string. Syntax : Pos(substr,s) Example : n:=pos(“are”,”Who are you ?”); { n:=5; } If the substring is not found, it returns 0.Val : converts strings to numeric. Syntax : val(strvar,numvar,errorcode) strvar is a string variable to be converted, numvar is any numeric variable either integer or real, and errorcode is an integer variable that holds the error code. If errorcode is 0, conversion success. Otherwise, it points at the position of strvar that cause failure. Example :var s : string; e : integer; r : real;begin write(“Enter a number : “); readln(s); val(s,r,e); if e0 then writeln(“Error at position : “,e); else writeln(“That was : “,r:4:3);end.Str : converts numeric into strings. Syntax : str(numvar,strvar) Example :var s : string; i : integer;begin write(“Input an integer : “); readln(i); str(i,s); writeln(“That was : “,s);end. Note that if you deal with real, you may format it before you convert it into strings. Suppose r is a real variable, s can be like this : 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.Concat : Concatenates two or more strings Syntax : concat(s1,s2,…,sn) Example : st:=concat(s1,s2); If s1=”ABC” and s2=”DEF”, st would be “ABCDEF” st:=concat(“Borland “,”Pascal “,”ver. “,”7.0”); Would be “Borland Pascal ver. 7.0” You may put as many parameters to concat as possible. If the resulting string length is more than 255, it will be truncated to 255. Concat is the same if we use plus sign (+). For example : st:=concat(“ABC”,”DEF”); is the same as st:=”ABC”+”DEF”;Insert : Insert a string inside another string from indexth character Syntax : insert(source,target,index) Example :var s1, s2 : string;begin s1:=”not “; s2:=”I do love you”; insert(s1,s2,6); writeln(s2); { I do not love you }end. If the result string length is more than 255, it will be truncated into 255 as well.Delete : Deletes n characters from string s starting from index i.

Xem thêm: Mẫu Giấy Ủy Quyền Giao Dịch Ngân Hàng Vietinbank, Giấy Ủy Quyền Giao Dịch Ngân Hàng Vietinbank

Syntax : delete(s,i,n); If index i is greater than length of s, s is unchanged. If n specifies more characters than remain (starting from i), the remainder of the string is deleted. Example :var s : string;begin s:=”I am not responsible for that !”; delete(s,6,3); writeln(s); { I am responsible for that }end.Fillchar : fill string s with character c until s is n-1 char long. Syntax : fillchar(s,n,c); Beware : s<0> is overrided, so don”t forget to add s<0>:=chr(n-1); to normalize it.var s : string;begin fillchar(s,51,”=”); s<0>:=chr(50);end.Actually, those procbocdau.comres or functions can be read from Pascal”s help. So,refer to Borland Pascal help if you want working examples. You can evenmake your own string functions. If you don”t understand, e-mailme.

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 isthat Pascal can not maintain strings larger than 255. Then a new type ofstring is employed : PChar, a pointer of character.

Pascal string consists of : one byte for its length then the contents. InPChar, just like C, we don”t recognize the length. All we know is when thestring stops — that is when we encounter ASCII 0 (NULL). That null stopstyle makes the C style of string called NULL TERMINATED STRING.

All string manipulation routines for PChar are adopted from C. If you wouldlike 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 secondlesson of Pascal.

Xem thêm: Irristat For Windows – Irristat Tutorial Pdf

You could compare two strings just like numbers, too ! Suppose s1 and s2are strings, you could do like this :

if s1 That”s all for this time.

Where to go ?Back to main pageBack to Pascal Tutorial Lesson 1 contentsTo the quizBack to Chapter 7 about arraysTo Chapter 9 about recordsMy page of programming linkContact me hereBy : Roby Joehanes, © 1996, 2000

Viết một bình luận