Naive Pattern Matching Algorithm C Program

By | March 10, 2017

Learn how to implement the Naive Pattern Matching Algorithm in C programming language using functions and for loop. The pattern matching algorithm is also known as String Searching Algorithm.

What is Pattern Matching Algorithm?

The Naive String Matching Algorithm is one of the simplest methods to check whether a string follows a particular pattern or not.

It is simple of all the algorithm but is highly inefficient. It checks where the string matches the input pattern one by one with every character of the string.

The complexity of the Naive String Search Algorithm for average case scenario is O(n +m) whereas for worst case scenario is O(nm).

The following Naive String Search program takes a string from the user and then a pattern that user wants to find in the string.

If the pattern is found in the string, it will display the position where the pattern matched in the string.

C Program For Naive Pattern Matching Algorithm using Functions and FOr Loop

There are many other algorithms primarily developed for pattern matching or string matching requirements. Some of them are as follows:

  1. Boyer Moore Search Algorithm
  2. Rabin Karp String Search Algorithm
  3. Two Way String Matching Algorithm
  4. Backward Non-Deterministic Dawg Matching Algorithm
  5. Finite State Automation Algorithm
  6. Knuth Morris Prat Algorithm

Must Read: C Program To Find Permutations of a String using Recursion

Method 1: C Program For Naive Pattern Matching Algorithm using For Loop


Must Read: C Program To Find Longest Common Subsequence

Method 2: C Program For Naive String Matching Algorithm using Function

Must Read: C Program To Compare Two Strings


Enter a String: CodingAlpha
Enter a Pattern to Match: in
Pattern Matched at Position: 3

If you have any compilation error or doubts in this Naive Pattern Matching program in C language, let us know about it in the comment section below.

2 thoughts on “Naive Pattern Matching Algorithm C Program

  1. Venkatesh PS

    Amazing. I didn’t knew string pattern matching could be so simple. Thanks guys.

  2. Ramesh Rajan

    Please upload Knuth Morris Prat algorithm for pattern matching in C programming.


Let's Discuss