pastebin

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