You are on page 1of 3

IMPLEMENTATION OF LAMPEL ZIV ENCODING

PROGRAM:
#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
int i,j,k,count,r,l,temp,b[20];
char a[20][10],t[10],str[20];
clrscr();
cout<<"Enter the no.of basic elements:";
cin>>r;
cout<<r<<"Characters\n";
count=1;
for(i=0;i<r;i++)
{
cin>>a[i];
b[i]=count;
count=count+1;
}
for(i=0;i<r;i++)
{
cout<<a[i]<<"="<<b[i];
cout<<"\n";
}
count=r;
cout<<"Enter the string to be encoded:";
cin>>str;
l=strlen(str);
i=0;
j=0;
x:
while(i<l)
{
t[j]=str[i];
t[j+1]='\0';
for(k=0;k<count;k++)
{
if(strcmp(t,a[k])==0)
{
i=i+1;
j=j+1;
temp=k;
goto x;

}
}
strcpy(a[count],t);
b[count]=b[count-1]+1;
cout<<b[temp];
j=0;
count=count+1;
}
cout<<b[temp];
cout<<"\n Dictionary Contents \n";
for(i=0;i<count;i++)
{
cout<<a[i]<<" = "<<b[i];
cout<<"\n";
}
getch();
}

OUTPUT:
Enter the no.of elements:4
4 Characters
a
b
v
d
a=1
b=2
v=3
d=4
Enter the string to be encoded: abvddvba
12344321
Dictionary Contents:
a=1
b=2
v=3
d=4
ab=5
bv=6
vd=7
dd=8
dv=9
vb=10
ba=11

You might also like