1 STRINGS String data type Basic operations on strings String functions String procedures.
-
Upload
gerald-barton -
Category
Documents
-
view
217 -
download
0
Transcript of 1 STRINGS String data type Basic operations on strings String functions String procedures.
![Page 1: 1 STRINGS String data type Basic operations on strings String functions String procedures.](https://reader036.fdocuments.in/reader036/viewer/2022082518/5697bfc21a28abf838ca5168/html5/thumbnails/1.jpg)
1
STRINGSSTRINGSString data type
Basic operations on strings
String functions
String procedures
![Page 2: 1 STRINGS String data type Basic operations on strings String functions String procedures.](https://reader036.fdocuments.in/reader036/viewer/2022082518/5697bfc21a28abf838ca5168/html5/thumbnails/2.jpg)
2
Definition of Strings
• character data– e.g., ‘A’, ‘B’, ‘C’, ‘x’, ‘y’, ‘z’, ‘!’, ‘*’, etc.
• sequences of characters– e.g., ‘computer programming’
![Page 3: 1 STRINGS String data type Basic operations on strings String functions String procedures.](https://reader036.fdocuments.in/reader036/viewer/2022082518/5697bfc21a28abf838ca5168/html5/thumbnails/3.jpg)
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]
![Page 4: 1 STRINGS String data type Basic operations on strings String functions String procedures.](https://reader036.fdocuments.in/reader036/viewer/2022082518/5697bfc21a28abf838ca5168/html5/thumbnails/4.jpg)
4
Basic Operations on Strings
• assignment
• input/output
• accessing individual characters of a string
• comparison
• initializing a string variable
![Page 5: 1 STRINGS String data type Basic operations on strings String functions String procedures.](https://reader036.fdocuments.in/reader036/viewer/2022082518/5697bfc21a28abf838ca5168/html5/thumbnails/5.jpg)
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);
![Page 6: 1 STRINGS String data type Basic operations on strings String functions String procedures.](https://reader036.fdocuments.in/reader036/viewer/2022082518/5697bfc21a28abf838ca5168/html5/thumbnails/6.jpg)
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
![Page 7: 1 STRINGS String data type Basic operations on strings String functions String procedures.](https://reader036.fdocuments.in/reader036/viewer/2022082518/5697bfc21a28abf838ca5168/html5/thumbnails/7.jpg)
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]
![Page 8: 1 STRINGS String data type Basic operations on strings String functions String procedures.](https://reader036.fdocuments.in/reader036/viewer/2022082518/5697bfc21a28abf838ca5168/html5/thumbnails/8.jpg)
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
![Page 9: 1 STRINGS String data type Basic operations on strings String functions String procedures.](https://reader036.fdocuments.in/reader036/viewer/2022082518/5697bfc21a28abf838ca5168/html5/thumbnails/9.jpg)
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
![Page 10: 1 STRINGS String data type Basic operations on strings String functions String procedures.](https://reader036.fdocuments.in/reader036/viewer/2022082518/5697bfc21a28abf838ca5168/html5/thumbnails/10.jpg)
10
String Functions
• length
• concat
• copy
• ord
• chr
![Page 11: 1 STRINGS String data type Basic operations on strings String functions String procedures.](https://reader036.fdocuments.in/reader036/viewer/2022082518/5697bfc21a28abf838ca5168/html5/thumbnails/11.jpg)
11
lengthlength
•length( <string> )
– returns the length of a <string>
– i.e., the number of characters in the string
![Page 12: 1 STRINGS String data type Basic operations on strings String functions String procedures.](https://reader036.fdocuments.in/reader036/viewer/2022082518/5697bfc21a28abf838ca5168/html5/thumbnails/12.jpg)
12
concatconcat
• concatenation
– the process of joining strings together to form longer string
• 2 methods:
– concat( <string1>, <string2>, ..., <stringN> )
– using concatenation operator, +
![Page 13: 1 STRINGS String data type Basic operations on strings String functions String procedures.](https://reader036.fdocuments.in/reader036/viewer/2022082518/5697bfc21a28abf838ca5168/html5/thumbnails/13.jpg)
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
![Page 14: 1 STRINGS String data type Basic operations on strings String functions String procedures.](https://reader036.fdocuments.in/reader036/viewer/2022082518/5697bfc21a28abf838ca5168/html5/thumbnails/14.jpg)
14
ord & chrord & chr
• ord( <character> )
– returns the ASCII value of a < character>
• chr( <code> )
– returns the ASCII character associated with that
<code>
![Page 15: 1 STRINGS String data type Basic operations on strings String functions String procedures.](https://reader036.fdocuments.in/reader036/viewer/2022082518/5697bfc21a28abf838ca5168/html5/thumbnails/15.jpg)
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
![Page 16: 1 STRINGS String data type Basic operations on strings String functions String procedures.](https://reader036.fdocuments.in/reader036/viewer/2022082518/5697bfc21a28abf838ca5168/html5/thumbnails/16.jpg)
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
![Page 17: 1 STRINGS String data type Basic operations on strings String functions String procedures.](https://reader036.fdocuments.in/reader036/viewer/2022082518/5697bfc21a28abf838ca5168/html5/thumbnails/17.jpg)
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
![Page 18: 1 STRINGS String data type Basic operations on strings String functions String procedures.](https://reader036.fdocuments.in/reader036/viewer/2022082518/5697bfc21a28abf838ca5168/html5/thumbnails/18.jpg)
18
The End