c program for Naive String Matching algorithm in DAA

Naive String Matching algorithm in DAA

The Naive String Matching algorithm is a simple method used to find occurrences of a pattern within a text.

Here’s how it works:

1. Start with the first character of the text.

2. Compare it with the first character of the pattern.

3. If they match, compare the next characters of both text and pattern.

4. If at any point a mismatch is found, shift the pattern one position to the right and start over from step 2.

5. Repeat this process until either the end of the text is reached or a complete match is found.

 

Naive String Matching algorithm in pseudocode:

In this pseudocode:

‘text’ is the input text where you want to find occurrences of the pattern.
‘pattern’ is the string you’re searching for within the text.
‘n’ is the length of the text.
‘m’ is the length of the pattern.

This algorithm has a time complexity of O((n-m+1)*m), where n is the length of the text and m is the length of the pattern.

 

Program code of Naive String Matching algorithm in DAA

Output

Enter a sentance : This is a simple example of the Naive String Matching algorithm @ coderevise.com
Enter Pattern to found with position : String
Pattern found at position: 38

 


 

Read Also

kmp string matching algorithm

Selection Sort

Heap Sort

Matrix Chain Multiplication in C

 

 

 

 

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top