/media/sda-magnetic/david/Dokumente-15-2023-11-22/informatik/newC2023-10-23/dev2023-10-23/lempelziv5.c


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

char table [1024][1024];
char tabledst [1024][1024];
int itable [1024];
int ctable [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 [] = "ABBACABABBAAACCAACAACACACBBBBACAABACBBABABCA";
    int coded [256*128];
    int x;
    
    for (n = 'A' - 'A';  n < ('C' - 'A')+1;  n++) {
      table [n][0] = n + 'A';
      table [n][1] = 0;
      ctable [n] = n + 'A';
      itable [n] = n;
    }
  
    
    for (i = 0, x = 0, j = 1, length = 1;  i < strlen (text)+1; ) {
      for (length = 1; (i + length) < strlen (text)+1; ) {
        for (k = 0;  (k < n) && (strncmp (table [k], text+i, length) != 0);  k++);
        if (k >= n) {
          coded [x] = notice;
          x++;
          itable [n] = notice;
          ctable [n] = text[i + length-1]; 
          strncpy (table [n], text+i, length);
          n++;
          length--;
          break;
        }
        else if (k < n) {
          notice = k;
          length++;
        }
      }
      i = i + length;
    }
    
    for (i = 0;  i < n;  i++) {
      printf ("%i: %i %c\n", i, itable [i], ctable [i]);
    }
    
   for (i = 0;  i < n;  i++) {
      if (itable [i] == i) {
        tabledst [i][0] = ctable [i];
        tabledst [i][1] = 0;
      }
      else {
        strcpy (tabledst [i], table [itable[i]]);
        tabledst [i][j = strlen(tabledst[i])+0] = ctable [i];
        tabledst [i][j+1] = 0;
      }
    }
    
    for (i = 0;  i < n;  i++) {
      printf ("%s, %s\n", table [i], tabledst [i]);
    }
  
      

    
return 0;
}