Theory:: Title

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Data Structures and Algorithms EE-223

Lab # 15
Data Structures and Algorithms

Title:
● Implementation of Naive String Matching in C++

Theory:
Naive String matching:
In computer science, string searching algorithms, sometimes called string matching
algorithms, are an important class of string algorithms that try to find a place where
one or several strings (also called patterns) are found within a larger string or text.

When we talk about a string matching algorithm, everyone can get a simple string
matching technique. That is starting from first letters of the text and first letter of
the pattern check whether these two letters are equal. If it is, then check second
letter of the text with the given pattern. If it is not equal, then move first letter of
the pattern to the second letter of the text. Then check these two letters.

Brute Force string matching algorithm is also like that. Therefore we call that as
Naive string matching algorithm. Naive means basic or simple.

Example #1

In above red boxes says mismatch letters against letters of the text and green boxes
says match letters against letters of the text. According to the above

Department of Electrical Engineering, WEC


Data Structures and Algorithms EE-223

In first row we check whether first letter of the pattern is matched with the first
letter of the text. It is mismatched, because "S" is the first letter of pattern and "T"
is the first letter of text. Then we move the pattern by one position. Shown in
second row.
Then check first letter of the pattern with the second letter of text. It is also
mismatched. Likewise we continue the checking and moving process. In fourth
row we can see first letter of the pattern matched with text. Then we do not do any
moving but we increase testing letter of the pattern. We only move the position of
pattern by one when we find mismatches. Also in last row, we can see all the
letters of the pattern matched with the same letters of the text.

Example #2
If we consider a sequence t = {CTCACTGCCTGCCTAG} and a pattern p =
{CTGCCTAG}, the pattern p is inside t from the ninth symbol, and we can say that
the pattern p appears inside t with movement s, in this case, s = 8.

Department of Electrical Engineering, WEC


Data Structures and Algorithms EE-223

Example #3
Write a function to perform Naive String matching algorithm.
void search(char * pattern, char *txt)
{
int M = strlen(pattern);
int N = strlen(txt);
for (int i = 0; i <= N - M; i++) /* A loop to slide pat[] one by one */
{
int j;
for (j = 0; j < M; j++) /* For current index i, check for pattern match */
{
if (txt[i + j] != pattern [j])
break;
}
if (j == M) // if pattern [0...M-1] = txt[i, i+1, ...i+M-1]
{
cout<<"Pattern found at index : "<< i<<endl;
}
}
}

Lab Task 1
Implement Naive String matching algorithm.

Lab Task 2
Write a program to search your name in the given string below by using Naive
String matching algorithm.

string str=”AHFDSLAOEDYOURNAMEKAWQPOILMD”;

Procedure:-
……………………………………………………………………………
……………………………………………………………………………

Department of Electrical Engineering, WEC


Data Structures and Algorithms EE-223

Conclusion and Comments:-


……………………………………………………………………………
……………………………………………………………………………

Department of Electrical Engineering, WEC

You might also like