DS Chapter 2
DS Chapter 2
String Processing
Aifa Faruque
Lecturer,CSE,NMDC
What is String?
Strings are considered a data type in general and are typically represented as
arrays of bytes (or words) that store a sequence of characters. Strings are
defined as an array of characters.
Different Type of strings operation
SUBSTRING(string,position,length)
INDEXING(text,pattern)
CONCATENATION
LENGTH(string)
Different Word Processing
Insertion:INSERT(text,position,string)
Deletion: DELETE(text,position,length)
Replacement: REPLACE(text,pattern1,pattern2)
Different Type of Structure for stored strings
3.Linked Structure
Write an algorithm that deletes every occurrences of a pattern ‘P’ in a text
‘T’.
Algorithm ( Delete)
A text T and pattern P are in memory. This algorithm deletes every occurrence of P in T.
1. K:= INDEX( T,P).
2. While ( k ≠ 0)
(a) [Delete P from T]
T=DELETE ( T, INDEX(T,P), LENGTH (P) )
(b) [Update index]
K= INDEX (T,P)
[End of Loop]
3. Write: T.
Exit.
Write an algorithm that replace every occurrence of P
in T by Q.
Algorithm ( REPLACE)
A text T and pattern P are in memory. This algorithm replace every
occurrence of P in T by Q.
1. K:= INDEX( T,P).
2. While ( k ≠ 0)
(a) T=REPLACE ( T, P,Q )
(b) K= INDEX (T,P)
[End of Loop]
3. Write: T.
Exit.
Pattern searching