Download presentation
Presentation is loading. Please wait.
Published byGerald Barton Modified over 9 years ago
1
1 STRINGS String data type Basic operations on strings String functions String procedures
2
2 Definition of Strings character data –e.g., ‘A’, ‘B’, ‘C’, ‘x’, ‘y’, ‘z’, ‘!’, ‘*’, etc. sequences of characters –e.g., ‘computer programming’
3
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
4 Basic Operations on Strings assignment input/output accessing individual characters of a string comparison initializing a string variable
5
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
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
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
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
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
10 String Functions length concat copy ord chr
11
11 length length( ) –returns the length of a –i.e., the number of characters in the string
12
12 concat concatenation –the process of joining strings together to form longer string 2 methods: –concat(,,..., ) –using concatenation operator, +
13
13 copy copy(,, ) –returns a substring with the special from the, beginning at position –used to cut off part of a string
14
14 ord & chr ord( ) –returns the ASCII value of a chr( ) –returns the ASCII character associated with that
15
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
16 val val(,, ) –converts to its numeric equivalent conversion –successful: - numeric value - the value 0 –failure: - set to 0 - the position of the 1st invalid character declaration error code
17
17 str str(, ) –converts a numeric value,, to its string representation –assigns this string value to field descriptors can be included to specify the string format –str( : :, ) width no. of decimal place
18
18 The End
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.