2. String Processing
2. String Processing
Rumana Yasmin
Lecturer
DCSE, FST, BUP
Chapter 2
Preliminaries
Mathematical Notations and Functions
• Floor Function
✓ Floor function of x denotes the greatest integer that does not exceed x
• Ceiling Function
✓ Ceiling Function of x denotes the least integer that is not less than x
String Processing
STRING
• Strings are defined as an array of characters.
• Substring
o A substring is a contiguous part of a string, i.e., a string inside another
string.
5
STRING OPERATIONS
• Substring
• Indexing
• Concatenation
• Length
6
SUBSTRING
• To access a substring from a given string we need:
1. The name of the string
2. The position of the first character of the substring in the given string
3. The length of the substring
7
INDEXING
• Also known as Pattern matching
• Finds the position where the string pattern (P) first appears in the given
string text (T)
INDEX(text, pattern)
8
CONCATENATION
• Let S1 and S2 be two strings.
• The concatenation of S1 and S2 (denoted by S1//S2) is the string
consisting of the characters of S1 followed by the characters of
S2.
• S1 = ‘MUSHFIQUR’ strcat()
• S2 = ‘RAHIM’
• S1//S2 = ‘MUSHFIQURRAHIM’
• S1// //S2 = ‘MUSHFIQUR RAHIM’
9
LENGTH
• The number of characters in the string is called its length.
LENGTH(string)
• S = ‘COMPUTER SCIENCE’
• LENGTH(S) = 16
strlen()
• S1 = ‘ ’ Space
• LENGTH(S1) = ?
• S2 = ‘\0’ null character
• LENGTH(S2) = ?
10
WORD/TEXT PROCESSING
• Insertion
• Insert a substring to a specific position
• Deletion
• Delete a substring from a specific position
• Replacement
• Replace the first occurrence of a pattern P1 by another pattern P2
11
WORD/TEXT PROCESSING
• INSERT(text, position, string)
• INSERT(‘ABCDEFG’, 3, ‘XYZ’) = ‘ABXYZCDEFG’
12
WORD/TEXT PROCESSING
• REPLACE(text, pattern1, pattern2)
• REPLACE(‘XABYABZ’, ‘AB’, ‘C’) = ‘XCYABZ’
• REPLACE(‘XABYABZ’, ‘BA’, ‘D’) = ‘XABYABZ’
13
Practice Problem
Let S and T be character variables such that
S = ‘WE ARE THE PEOPLE ’
T = ‘OF THE BANGLADESH’
1. Find the length of S and T
A = SUBSTRING(S, 8, 6)
B = INSERT(A, 7, ‘BGDXY’)
C = DELETE(B, 11, 2)
D = REPLACE(C, ‘BGD’, T)
2. Find the values of A, B, C, and D
14
First Pattern Matching Algorithm
• Also known as “Slow Pattern Matching Algorithm”