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


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

char table [1024*128][1024];
char tabledst [1024*128][1024];
int itable [1024*128];
char ctable [1024*128];

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 [1024*64]; 
    int coded [1024*64];
    int x;
    
    srand (0);
    
    for (i = 0;  i < 1024;  i++)
      text [i] = (rand () % ('c'-'a')) + 'a';
    text [i] = 0;
    
    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]);
    }
  
    printf ("text: %i\n", strlen (text));
    printf ("coded: %i\n", x+n+n);
    
return 0;
}