0% found this document useful (0 votes)
15 views17 pages

Chapter 3

Uploaded by

Ahnaf Abid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views17 pages

Chapter 3

Uploaded by

Ahnaf Abid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Chapter 3

String
3.2 BASIC TERMINOLOGY

Each programming language contains a character set that is used to communicate


with the com- puter. This set usually includes the following:
Alphabet: ABCDEFGHIJKLMNOPQRSTUVWXYZ
Digits: 0 1 2 3 4 5 6
7 8 9
Special characters: + - / * ( ) ,
. $ = '
Substring
If X is an empty string, then Y is called an initial substring of S, and if Z is an
empty string then Y is called a terminal substring of S. For example,

'BE OR NOT' is a substring of 'TO BE OR NOT TO BE'

'THE' is an initial substring of 'THE END’


STORING STRINGS

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:

To be or not to be, that is the question.


String Operations
GETCHAR(str, n) Returns the nth character in the string

PUTCHAR(str, n, c) Sets the nth character in the string to c

LENGTH(str) Returns the number of characters in the string

POS(str1, str2) Returns the position of the first occurrence of str2 found in strl, or 0
if no match

CONCAT(strl, str2) Returns a new string consisting of characters in strl


followed by characters in str2

SUBSTRING(str1, i, m) Returns a substring of length m starting at position i in string str

DELETE(str, i, m) Deletes m characters from str starting at position i

INSERT(str1, str2, i) Changes str1 into a new string with str2 inserted in position i

COMPARE(str1, str2) Returns an integer indicating whether strl > str2


Insertion
Deletion
Algorithm for deletion of every occurance
Replace
Algorithm for replacement of every occurance
Pattern matching
WK = SUBSTRING(T, K, LENGTH(P))

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

First Pattern Matching Algorithm is solvable in Polynomial Time


Second Pattern Matching Algorithm is solvable in Linear Time

You might also like