User Tools

Site Tools


tanszek:oktatas:techcomm:lzw_coding

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
tanszek:oktatas:techcomm:lzw_coding [2024/10/07 08:26] – [Decoding LZW] kneheztanszek:oktatas:techcomm:lzw_coding [2024/11/19 07:54] (current) knehez
Line 10: Line 10:
  
 3. **Add new substrings** to the dictionary and output the dictionary index for the current substring when it can no longer be extended by the next character in the input. 3. **Add new substrings** to the dictionary and output the dictionary index for the current substring when it can no longer be extended by the next character in the input.
 +----
  
 === Example of LZW Compression === === Example of LZW Compression ===
Line 123: Line 124:
  
  
 +C implementation of LZW encoding:
  
 +<sxh c>
 +#include <stdio.h>
 +#include <string.h>
 +
 +#define MAX_sxhS 100
 +
 +char sxhTable[MAX_sxhS][10] = {"a","b","c","d","e"};
 +
 +int tableElements = 5;
 +
 +int isInsxhTable(char *s)
 +{
 +    for(int i = 0; i < tableElements; i++)
 +    {
 +        if(strcmp(sxhTable[i], s) == 0)
 +        {
 +            return i;
 +        }
 +    }
 +    return -1;
 +}
 +
 +int main()
 +{
 +    char text[] = "dabbacdabbacdabbacdabbacdee";
 +    char *p = text;
 +    int bufferEnd = 1;
 +
 +    int lastFoundIndex = -1;
 +    while(*p != '\0')
 +    {
 +        char subStr[10];
 +        strncpy(subStr, p, bufferEnd);
 +        subStr[bufferEnd] = '\0';
 +        int foundIndex = isInsxhTable(subStr);
 +        if(foundIndex != -1)
 +        {
 +           bufferEnd++;
 +           lastFoundIndex = foundIndex;
 +           continue;
 +        }
 +        p += strlen(subStr) - 1;
 +        bufferEnd = 1;
 +        strcpy(sxhTable[tableElements++], subStr);
 +        printf("%d,", lastFoundIndex + 1);
 +    }
 +}
 +
 +</sxh>
tanszek/oktatas/techcomm/lzw_coding.1728289612.txt.gz · Last modified: 2024/10/07 08:26 by knehez