C Program for Pattern Matching


In C Programing, Pattern matching is the way of checking a series of pattern or a sequence of digits or string with some other pattern and find out if it matches or not, in pattern recognition, the match usually has to be exact.

A pattern can be a series of digits, a string, different types of colors arranged in order. The order is really important in case of pattern matching.

We begin the code with including the header files “stdio.h” and “conio.h” which helps in input and output of data and holds the output screen.

We used an array to input a string of numbers one by one. If you are using an array to input string of numbers, you have to count the actual number of digits present in the string and input that number.

After counting the number we now input the digits one by one which will then be stored in the array “no[]”

After input of the main string, we follow the same steps for the sub-string which is to be matched,

We count the number of digits in sub-string too and then input the string.

From here the main part begins, initialize a for loop ‘i’ and run it until the value is less than or equal to the number of digits in the main string and take another for-loop ‘j’ inside it and run it until it is less than or equal to the number of digits in the sub-string.

Now check if all the values of first sub-string or digits of sub-string are found in the main string anywhere in continuation then increment the value of variable “c” and after the termination of the loop check if the value of “c==f1” then print “substring found”.

Else we initialize the value of “c=0” and increment the value of first loop and repeat the process. On the end of the first loop if nowhere the pattern is found then the value of c will never be equal to the number of digits in sub-string and hence “substring not found” will be displayed on the output screen.

I have used a sequence of digits or a string of digits and initialize it using array, you can use different kind or pattern for running pattern matching like strings, etc. most important thing is to understand the logic.

c-program-for-pattern-matching-1 c-program-for-pattern-matching-2 c-program-for-pattern-matching-3

C Program for Pattern Matching Output

c-program-for-pattern-matching-4 c-program-for-pattern-matching-5

Download: Source Code C Program for Pattern Matching