Chapter 3
Chapter 3
String
3.2 BASIC TERMINOLOGY
Generally speaking, strings are stored in three types of structures: (1) fixed-length
structures, (2) variable-length structures with fixed maximums and (3) linked
structures. We discuss each type of structure separately, giving its advantages
and disadvantages.
Fixed length
Variable length
Linked List
Strings may be stored in linked lists as follows. Each memory cell is assigned one
character or a fixed number of characters, and a link contained in the cell gives the
address of the cell containing the next character or group of characters in the
string. For example, consider this famous quotation:
POS(str1, str2) Returns the position of the first occurrence of str2 found in strl, or 0
if no match
INSERT(str1, str2, i) Changes str1 into a new string with str2 inserted in position i
Let W denote the substring of T having the same length as P and beginning with
the Kth character of T.
● First we compare P, character by character, with the first substring, W₁. If all the
characters are the same, then P = W, and so P appears in T and INDEX(T, P) = 1.
● On the other hand, suppose we find that some character of P is not the same as the
corresponding character of W₁. Then PW, and we can immediately move on to the
next substring. W2.
● We next compare P with W2. If PW2, then we compare P with W3, and so on. The
process stops (a) when we find a match of P with some substring Wx and so P
appears in T and INDEX(T, P) = K, or (b) when we exhaust all the Wk's with no match
and hence P does not appear in T. The maximum value MAX of the subscript K is
equal to LENGTH(T) - LENGTH(P) + 1
Pattern matching
First Pattern Matching Algorithm
Second Pattern Matching Algorithm P = aaba