You are here

C program for pattern matching

Pattern matching in C: C program to check if a string is present in an another string, for example, the string "programming" is present in the string "C programming". If it is present, then it's location (i.e. at which position it is present) is printed. We create a function match which receives two character arrays and returns the position if matching occurs otherwise returns -1. We are implementing naive string search algorithm in this program.

C programming code

  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. int match(char [], char []);
  5.  
  6. int main() {
  7.   char a[100], b[100];
  8.   int position;
  9.  
  10.   printf("Enter some text\n");
  11.   gets(a);
  12.  
  13.   printf("Enter a string to find\n");
  14.   gets(b);
  15.  
  16.   position = match(a, b);
  17.  
  18.   if (position != -1) {
  19.     printf("Found at location: %d\n", position + 1);
  20.   }
  21.   else {
  22.     printf("Not found.\n");
  23.   }
  24.  
  25.   return 0;
  26. }
  27.  
  28. int match(char text[], char pattern[]) {
  29.   int c, d, e, text_length, pattern_length, position = -1;
  30.  
  31.   text_length    = strlen(text);
  32.   pattern_length = strlen(pattern);
  33.  
  34.   if (pattern_length > text_length) {
  35.     return -1;
  36.   }
  37.  
  38.   for (c = 0; c <= text_length - pattern_length; c++) {
  39.     position = e = c;
  40.  
  41.     for (d = 0; d < pattern_length; d++) {
  42.       if (pattern[d] == text[e]) {
  43.         e++;
  44.       }
  45.       else {
  46.         break;
  47.       }
  48.     }
  49.     if (d == pattern_length) {
  50.       return position;
  51.     }
  52.   }
  53.  
  54.   return -1;
  55. }

Output of program:
Pattern matching C program

Download Pattern matching program.

C program for pattern matching using pointers

  1. #include<stdio.h>
  2.  
  3. int match(char*, char*);
  4.  
  5. int main()
  6. {
  7.    char a[100], b[100];
  8.    int position;
  9.    
  10.    printf("Enter some text\n");
  11.    gets(a);
  12.        
  13.    printf("Enter a string to find\n");
  14.    gets(b);
  15.    
  16.    position = match(a, b);
  17.    
  18.    if(position!=-1)
  19.       printf("Found at location %d\n", position+1);
  20.    else
  21.       printf("Not found.\n");
  22.      
  23.    return 0;  
  24. }
  25.  
  26. int match(char *a, char *b)
  27. {
  28.    int c;
  29.    int position = 0;
  30.    char *x, *y;
  31.    
  32.    x = a;
  33.    y = b;
  34.    
  35.    while(*a)
  36.    {
  37.       while(*x==*y)
  38.       {
  39.          x++;
  40.          y++;
  41.          if(*x=='\0'||*y=='\0')
  42.             break;        
  43.       }  
  44.       if(*y=='\0')
  45.          break;
  46.          
  47.       a++;
  48.       position++;
  49.       x = a;
  50.       y = b;
  51.    }
  52.    if(*a)
  53.       return position;
  54.    else  
  55.       return -1;  
  56. }