Preview
String
– String is a simple array of chars (characters)
– String is one-dimensional array of char
– The null character ‘\0’ must be included at the end of String
– Various built-in functions are provided for String function
1
Data Structures and Algorithms
9
String Operation
• Length: LENGTH (string)
e.g.- LENGTH(‘Mark Zuckerberg’)= 15
• Substring: SUBSTRING(string, initial, length)
e.g.- SUBSTRING(‘Impossible is a word found in coward’s dictionary’,0,20) =
Impossible is a word
• Indexing: INDEX(string, pattern)
e.g.- INDEX(‘He is wearing glasses’, ‘ear’)= 7
• Concatenation: String1//String2
e.g.- ‘To be or not to be’// ‘, this is the question.’= To be or not to be, this is the
question
String Operation
• Word Processing-
Insertion: INSERT(string, position, string)
e.g.- INSERT(‘ABCDEIJKL’,5,‘FGH’)=
ABCDEFGHIJKL
Deletion: DELETE(string, position, length)
e.g.- DELETE(‘ABCDEFG’, 4, 2)= ABCDG
Data Structures and Algorithms
11
String Operation
– Replacement: REPLACE(string, pattern1, pattern2)
e.g.- REPLACE(‘XABYABZ’, ‘AB’, ‘c’)= XCYABZ
REPLACE function can be executed be using the following three steps-
1. K:= INDEX(string, P1)
2. T:= DELETE(string, K, LENGTH(P1))
3. INSERT(T, K, P1)
– So, the algorithm is-
Data Structures and Algorithms
12
String Operation
– Pattern Matching:
Pattern matching is the problem of deciding whether or not a given string
pattern P appears in a text.
Widely used in word processing.
– So, a basic algorithm is-
Deletion: DELETE(string, position, length)
Algorithm for deletion -
Deletion example-