You are on page 1of 7

PART A

(PART A: TO BE REFFERED BY STUDENTS)

Experiment No.09
A.1 Aim:
Write a program to implement naïve string matching algorithm.

A.2 Prerequisite:

A.3 Outcome:

After successful completion of this experiment, students will be able to demonstrate


different string matching techniques.

A.4 Theory:

It employs a Brute Force technique to identify the presence of a pattern in the given


text.It is not efficient algorithm because it takes the time complexity of the algorithm to
check for a substring is O((m-n)n) where m is the length of the text and n is the length of
the pattern (substring) to be searched. There is no preprocessing required for this
algorithm, unlike KMP algorithm.

The idea of the naive solution is just to make a comparison character by character of the
text T[s...s + m − 1] for all s ∈ {0, . . . , n − m + 1} and the pattern P[0...m − 1]. It returns
all the valid shifts found.

Algorithm:

NAIVE-STRING-MATCHER(T, P)
n length[T]
m length[P]
for s 0 to n - m
do if P[1 . . m] = T[s + 1 . . s + m]
then print "Pattern occurs with shift" s
Example:
Time Complexity:

The time complexity of the Naive algorithm in its worst case is O(M × N). For example if
the pattern to search is a m and the text is a n, then we need M operation of comparison
by shift. For all the text, we need (N − M + 1) × M operation, generally M is very small
compared to N, it is why we can simply considered the complexity as O(M × N).
PART B
(PART B : TO BE COMPLETED BY STUDENTS)

(Students must submit the soft copy as per following segments within two hours of the
practical. The soft copy must be uploaded on the Blackboard or emailed to the concerned lab in
charge faculties at the end of the practical in case the there is no Black board access available)

Roll No. : Name:


Class:SE Batch:
Date of Experiment: Date of Submission:
Grade:

B.1 Software Code written by student:

Code:-

// C program for Naive Pattern Searching algorithm

#include<stdio.h>
#include<string.h>

int NaiveStringMatch(char Text[100],char Pattern[100]);

int main()
{
char Text[100],Pattern[100];
int status;

printf("Enter the Text:--> \n");


gets(Text);
printf("Enter the Pattern:--> \n");
gets(Pattern);
status = NaiveStringMatch(Text,Pattern);
if(status == -1)
printf("\n Match Not Found!");
else
printf("\n Match Found on %d position! ",status);
return 0;
}

int NaiveStringMatch(char Text[100],char Pattern[100])


{
int n,m,i,j,count=0,temp=0;
n=strlen(Text);
m=strlen(Pattern);
for(i=0;i<=n-m;i++)
{
temp++;
for(j=0;j<m;j++)
{
if(Text[i+j] == Pattern[j])
count++;
}
if(count==m)
return temp;
count=0;
}
return -1;

B.2 Input and Output:


B.3 Observations and learning:
(Students are expected to comment on the output obtained with clear observations and learning
for each task/ sub part assigned)

B.4 Conclusion:
(Students must write the conclusion as per the attainment of individual outcome listed above and
learning/observation noted in section B.3)

B.5 Question of Curiosity


(To be answered by student based on the practical performed and learning/observations)

Q.1: What are different string matching algorithm?

Ans:-the different algorithm are:-

 Brute Force algorithm, Knuth-Morris-Pratt algorithm


(KMP), Boyer Moore algorithm and Rabin Karp algorithm

Q.2: Why naïve string matching algorithm is not said to be an efficient algorithm?

Ans:-

The number of comparisons in the worst case is O(m*(n-m+1)). Although strings which have repeated
characters are not likely to appear in English text, they may well occur in other applications (for example,
in binary texts). The KMP matching algorithm improves the worst case to O(n). We will be covering KMP
in the next post. Also, we will be writing more posts to cover all pattern searching algorithms and data
structures.

Q.3: What are application areas of string matching algorithms?

Ans:- It is used for sorting a string which is being sent by the user.
************************

You might also like