#include<stdio.h>
#include<conio.h>
#include<string.h>
#define max 20
char cstr[max];
char dstr[max];
char str[max];
int main()
{
int opt;
int ret;
int i,j,k,len;
int pos;
int ind;
do
{
printf("\n 1. Compress the file");
printf("\n 2. DeCompress the file");
printf("\n 3. Display Compressed file");
printf("\n 4. Display DeCompressed file");
printf("\n\nEnter the option : ");
scanf("%d",&opt);
fflush(stdin);
switch(opt)
{
case 1: printf("\n Enter string : ");
gets(str);
fflush(stdin);
len = 0;
ind = 0;
i=0;
do
{
pos = i;
do
{
i++;
ind++;
}while(str[pos] == str[i]);
if(ind < 3)
{
for(j=0;j<ind;j++)
cstr[len++] = str[i-1];
}
else
{
cstr[len++] = '!';
cstr[len++] = str[i-1];
cstr[len++] = 48+ind;
}
ind = 0;
}while(i != strlen(str));
cstr[len] = '\0';
printf("\n\n\t The Text Data has been Compressed \n");
break;
case 2: len = 0;
i=0;
do
{
if(cstr[i] == '!')
{
ind = cstr[i+2]-48;
for(j=0;j<ind;j++)
dstr[len++] = cstr[i+1];
i += 3;
}
else
{
dstr[len++] = cstr[i++];
}
}while(i != strlen(cstr));
dstr[len] = '\0';
printf("\n\n\t The Compressed Text Data has been DeCompressed \n");
break;
case 3: printf("\n The Compressed string : ");
puts(cstr);
break;
case 4: printf("\n The DeCompressed string : ");
puts(dstr);
default: break;
}
}while(opt != 0);
getch();
return 0;
}
No comments:
Post a Comment