strings

advertisement
CHAPTER 9 : STRINGS
9.1
STRING DATA TYPE
String Constant
A sequence of characters enclosed in single quotation marks.
Example :
writeln(’Quick Pascal’);
String variable
A standard identifier used in VAR declaration section. String[n] declared in VAR means that the
variable can hold up to n characters. (Default is set to 255 characters)
Example :
VAR
name :
string[25];
{name can hold up to 25 characters}
address :
string;
{address can hold up to 255 characters}
9.2
BASIC OPERATIONS ON STRINGS
9.2.1 Comparison of Strings
String are similar to the numeric data which are ordered and can be compared using the relational
operators.
Operator
=
<>
<
<=
>
>=
Description
equal
not equal to
less than
less than or equal to
greater than
greater than or equal to
The order relations among characters are based on their ASCII codes. A character is defined to be
smaller than another character if its ASCII code is similar. Similarly, a string is less than another string if
the ASCII codes of the characters of the first string is smaller than the ASCII code of the corresponding
character of the second string when comparing successively from the beginning, or the first string is
shorter than the second string.
Examples :
’COMPUTER’
’COMPUTER ’
’aBCD’
’HELL’
’155’
=
>
>
<
<
’COMPUTER’
’COMPUTER’
’AXYZ’
’HELLO’
’25’
9.2.2 Assignment
A value can be assigned to the string variable by assignment statement. Any excess character (i.e. length
of character is longer than it declared) will be truncated.
9.2.3 Input / Output
A string variable can be read by input statement. i.e. readln or read
A string variable can be written by output statement. i.e. writeln or write
PYC/NOTES/CS2000/PP2K09/CWC
P. 1
9.2.4 Accessing individual characters of a string
A string can be considered as a packed array of characters. We can reference individual characters in the
string just as we reference individual elements in an array.
Example :
……
VAR
name :
string;
BEGIN
write(’Please input your name : ’);
readln(name);
writeln(’Your name is ’, name);
writeln(’The second character of your name is ’, name[2]);
END.
If your name is ’SUPERMAN’, try to work out the above example.
9.3
STRING FUNCTIONS
9.3.1 String Concatenation
Joining two or more strings into one single string.
String concatenation function (CONCAT)
Syntax :
FUNCTION CONCAT (string1, string2, ……: STRING) : STRING;
Function :
To join the string1, string2, …… in sequence to form a single string.
Example :
fullname := CONCAT(surname, othername);
address := CONCAT(room, estate, district, area);
String addition function (+)
Syntax :
[string1] + [string2]
Function :
To join two strings.
Example :
’CH’ + ’ENG’
’12’ + ’34’
’Hong’ + ’Kong’
is equal to
is equal to
is equal to
’1’ + ’ ’ + ’3’
is equal to
9.3.2 ASCII Character Function (CHR)
Syntax :
FUNCTION CHR (ASCII_code : INTEGER) : CHAR;
Function :
To return one ASCII code equivalent character, the input ASCII code must lie between 1
and 255.
PYC/NOTES/CS2000/PP2K09/CWC
P. 2
Example :
CHR(65)
CHR(65) + CHR(66)
FOR i := 32 to 255 DO
is equal to
is equal to
WRITE (i : 5, CHR(i) : 5);
9.3.3 ASCII Value Function (ORD)
Syntax :
FUNCTION ORD (character : CHAR) : INTEGER;
Function :
To return the equivalent ASCII ordinal number of a character.
Example :
ORD(’A’)
ORD(’1’)
ORD(’ ’)
ORD(CHR(65))
CHR(ORD(’B’))
is equal to
is equal to
is equal to
is equal to
is equal to
WRITE(’Type in the class : Form 4’);
READLN(class);
class_code := ORD(class) - 64;
FOR i := 1 to 40 DO
BEGIN
WRITE(’Type in mark of student ’, i , ’ : ’);
READLN(mark[class_code, i]);
END;
Classwork :
When the following program is executed, {10} is inputted and the random number sequence is:
{0.1, 0.5, 0.2, 0.9}, write down the outputs
PROGRAM exercise (INPUT, OUTPUT);
VAR
level, i, temp : INTEGER;
code, s
: STRING;
BEGIN
WRITE (’Input the game level : ’);
READLN(level);
FOR i := 1 TO 4 DO
code := code + CHR(TRUNC(RANDOM*level + 1) + 64 );
WRITELN(’This code is : ’,code);
END.
9.3.4 Length of String Function (LENGTH)
Syntax :
FUNCTION LENGTH(parameter : string) : INTEGER;
Function :
To return the number of characters of the string.
PYC/NOTES/CS2000/PP2K09/CWC
P. 3
Example :
LENGTH(’ABC’)
LENGTH(’ ABC’)
LENGTH(’A BC’)
is equal to
is equal to
is equal to
LENGTH(’ABC ’)
LENGTH(’ ’)
LENGTH(’ ’)
is equal to
is equal to
is equal to
(note :’’ means null string)
A := ’Hong’ ;
B := ’Kong’ ;
C := CONCAT(A, ’ ’, B) ;
WRITELN(LENGTH(C);
9.3.5 Substring Function (COPY)
Syntax :
FUNCTION COPY (s : STRING ; i, n : INTEGER) : STRINGS;
Function :
To return a substring of the string, s, starting from the i-th character of s and consisting n
characters.
Example :
COPY(’A
COPY(’A
COPY(’A
COPY(’A
CD’, 2, 1)
CD’, 3, 5)
CD’, 5, 2)
CD’, 3, 0)
is equal to
is equal to
is equal to
is equal to
Exercise 9.1:
(1)
Trace the following program and complete the output.
record := ’D145786PETER LAMM’;
id
:= COPY(record, 1, 7);
name := COPY(record, 8, 9);
sex
:= COPY(record, 17, 1);
WRITELN(’ID. NO. : ’, id);
WRITELN(’NAME : ’, name);
WRITELN(’SEX
: ’, sex);
OUTPUT :
(2)
Write a procedure, split, to accept a sentence and output the words in the sentence vertically.
E.g.
split (’This is an example.’);
the output would be : This
is
an
example.
PYC/NOTES/CS2000/PP2K09/CWC
P. 4
9.4
STRING PROCEDURES
9.4.1 Convert string procedure (STR)
Syntax
:
PROCEDURE STR (n : INTEGER; VAR s : string);
Function
:
To convert the value of the arithmetic expression, n, into a string, s.
Example
:
A := 1;
B := 23;
STR (A, S1);
STR (B, S2);
STR (A + B, S3);
WRITELN(S1 + S2);
WRITELN(S3);
9.4.2 Convert value procedure (VAL)
Syntax
:
PROCEDURE VAL (s:string; VAR n:num_type; VAR f:INTEGER);
Function
:
To convert a string, s, into a numeric value, n.
If the conversion is successful, the returned value of f is zero, otherwise, it will
contain the position of the string where error occurs.
Note
:
The data type of the variable n may either be integer or real, but it must be the
same as the type of the value converted.
Example
:
VAL (s, n, f)
VAL(’401’, n, f)
VAL(’+6’, n, f)
s
’401’
’+6’
n
401
6
f
0
0
VAL(’ -53’, n, f)
VAL(’ 9 ’, n, f)
VAL(’A88’, n, f)
VAL(’2.4E-02’, n, f)
VAL(’-2.4E+2’, n, f)
VAL(’2.4E2’, n, f)
VAL(’2.4E+W’, n, f)
VAL(’2.4E+2’, n, f)
VAL(’-2.4E +2’, n, f)
VAL(’2.4 E+2’, n, f)
VAL(’2+1’, n, f)
’ -53’
’ 9 ’
’A88’
’2.4E-02’
’2.4E2’
’2.4E+W’
’2.4E+W’
’2.4E+2 ’
’-2.4EE +2’
’ 12.3’
’2+1’
-53
0
0
0.0024
-240.0
240.0
0.0
0.0
0.0
0.0
0.0
0
3
1
0
0
0
6
7
6
4
2
Exercise 9.2:
Write a procedure inp_integer to accept an integer which reject all the invalid input with non-digit characters.
E.g.
inp_integer (num) will accept an integer from keyboard and store the integer in variable num.
Type in an integer
Invalid integer !
: A88
Type in an integer
: 88
PYC/NOTES/CS2000/PP2K09/CWC
P. 5
Download