Upload
gerald-barton
View
228
Download
0
Embed Size (px)
Citation preview
1
STRINGSSTRINGSString data type
Basic operations on strings
String functions
String procedures
2
Definition of Strings
• character data– e.g., ‘A’, ‘B’, ‘C’, ‘x’, ‘y’, ‘z’, ‘!’, ‘*’, etc.
• sequences of characters– e.g., ‘computer programming’
3
String Data Type
• string constant– a sequence of characters enclosed in single
quotation marks - ‘ ’– quotation marks themselves are not– e.g., ‘John’ ‘s computer’
• string variable– a predefined type identifier in Pascal– e.g., var Student : string[30]
4
Basic Operations on Strings
• assignment
• input/output
• accessing individual characters of a string
• comparison
• initializing a string variable
5
1. Assignment
• assigned value length < declared length– no problem
• assigned value length > declared length– the value is truncated to the declared length– only the leftmost characters are retained
• Exercise - what is the output of:– writeln(Name, class);
6
2. Input/output
• Input– use readln statement– input string value : no need to enclosed in ‘ ’
except this case, ‘bbbABC Company’
• Output– use write, or writeln statements
leading spaces
7
3. Accessing individual characters
• a string can be treated as a special array of characters
• example:var Name : string;
..
Name := ‘Jacky’;
writeln(Name[2]);
‘J’
‘a’
‘c’
‘k’
‘y’
Name[1]
Name[2]
Name[3]
Name[4]
Name[5]
8
4. Comparison
• strings are compared with <, >, =, <=, >= and <>
• two characters are compared with one another according to their ASCII value
• two strings are compared with one another character by character
9
5. Initializing a string variable
• an uninitialized string may contain
unpredictable value
• initialize a string variable with a
null string:
Sentence := ‘’no space in between
10
String Functions
• length
• concat
• copy
• ord
• chr
11
lengthlength
•length( <string> )
– returns the length of a <string>
– i.e., the number of characters in the string
12
concatconcat
• concatenation
– the process of joining strings together to form longer string
• 2 methods:
– concat( <string1>, <string2>, ..., <stringN> )
– using concatenation operator, +
13
copycopy
• copy( <string>, <start>, <size> )
– returns a substring with the special
<size> from the <string>, beginning at
position <start>
– used to cut off part of a string
14
ord & chrord & chr
• ord( <character> )
– returns the ASCII value of a < character>
• chr( <code> )
– returns the ASCII character associated with that
<code>
15
String Procedures
• some useful procedures are pre-declared and can be used by the program writers at any time they want– e.g., read, readln, write and writeln
• those concerning string are:– val– str
16
valval
• val( <string>, <num>, <ecode> )– converts <string> to its numeric equivalent
• conversion– successful:
• <num> - numeric value
• <ecode> - the value 0
– failure:• <num> - set to 0
• <ecode> - the position of the 1st invalid character
• declaration
error code
17
strstr
• str( <num>, <string> )– converts a numeric value, <num>, to its string
representation – assigns this string value to <string>
• field descriptors can be included to specify the string format– str( <num>:<w>:<d>, <string> )
widthno. of decimal place
18
The End