Re: Pattern Matching

Oder

#include <stdio.h>
#include <string.h>

char table [1024][1024];

int strnmcmp (char *src, char *des, int m, int n) {
  int i, j;
  for (i = m, j = 0;  i <= n; i++, j++)
    if (src [j] != des [i])
      return -1;
return 0;
}

//  strncat (p, src+m, n-m);

int main (void) {
    int ch;
    int i, j, k, n, length, notice;
    int c;
    char text [] = "ABBACABABB";

    for (n = 'A' - 'A';  n < ('C' - 'A')+1;  n++) {
      table [n][0] = n + 'A';
      table [n][1] = 0;
    }


    for (i = 0, j = 1, length = 1;  i < strlen (text); ) {
      for (length = 1; (i + length) < strlen (text); ) {
        for (k = 0;  (k < n) \&amp;\&amp; (strncmp (table [k], text+i, length) != 0);  k++);
        if (k >= n) {
          printf ("%i %sn", notice, table [notice]);
          strncpy (table [n], text+i, length);
          n++;
          length--;
          break;
        }
        else if (k < n) {
          notice = k;
          length++;
        }
      }
      i = i + length;
      length = 1;
    }
    for (i = 0;  i < n; i++) {
      printf ("%sn", table [i]);
    }

return 0;
}

david@works:~\$ ./a.out
0 A
1 B
1 B
0 A
2 C
3 AB
A
B
C
AB
BB
BA
AC
CA
ABA
david@works:~\$