Text Compression
Text Compression
AIM :
To implement the text compression algorithm using a C coding.
ALGORITHM :
Step 1 : Start.
Step 2 : Create the input file and get the text to be compressed as input from the user.
Step 3 : Using the file pointer scan the input file.
Step 4 : Compress the text using the algorithm.
Step 5 : Print the compressed code in the output file.
Step 6 : Stop.
PROGRAM :
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int a[20],b[20],c[20],d[20],e[10],k[10],q[10],r[10];
int max,m,n,temp,temp1,x=0,y=0,count=0,count1=0;
char c1,u;
char /*c1='A',*/a1,b1;
char
z[20][10]={{"0"},{"1"},{"00"},{"01"},{"10"},{"11"},{"000"},{"001"},{"010"},{"011"},{"1
00"},{"101"},{"110"},{"111"}};
int i,j;
FILE *fp;
char alp;
clrscr();
fp=fopen("HUFF.TXT","r");
while((c1=fgetc(fp))!='$')
count++;
fclose(fp);
fp=fopen("HUFF.TXT","r");
for(i=0;i<count;i++)
{
c1=fgetc(fp);
a[i]=c1;
}
for(i=0;i<count;i++)
{
for(j=0;j<count;j++)
{
if(a[i]<a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
for(i=0;i<count;i++)
{
for(j=0;j<count;j++)
{
if(a[j]==u) count++;
}
b[i]=count1;
u++;
count1=0;
}
i=0;
while(b[i]!=0)
{
y++;
i++;
}
for(i=0;i<y;i++)
{
if(b[i]!=0)
d[i]=b[i];
x++;
}
alp=a[0];
x=0;
k[0]=a[0];
for(i=1;i<count;i++)
{
if(a[i]!=alp)
{
k[++x]=a[i];
alp=a[i];
}
}
for(i=0;i<x+1;i++)
{
for(j=0;j<i;j++)
{
if(d[i]==d[j])
{
temp=d[i];
d[i]=d[j];
d[j]=temp;
}
temp1=k[i];
k[i]=k[j];
k[j]=temp1;
fclose(fp);
fp=fopen("HUFF.TXT","r");
printf("\nCode After Huffmam Compression");
for(i=0;i<count;i++)
{
b1=fgetc(fp);
for(j=0;j<x;j++)
{
if(b1==k[j])
printf("%s",z[j]);
}
getch();
}
}
}
}
OUTPUT :
Text input : AAABBBCCC$
Coded output : 1110001111