Vous êtes sur la page 1sur 6

Ex no:1

IMPLEMENTATION OF TEXT COMPRESSION USING RUN LENGTH ENCODING

AIM: To write a program to perform text compression algorithm using run length encoding technique. ALGORITHM: Start the program. Write some text in the input file Using the text in input file & compress the content using run length encoding technique. Write the compressed text (i.e.) translated into another form into output file. Stop the execution. PROGRAM
#include<stdio.h> #include<conio.h> void main() { FILE *fp1,*fp2; int i=1; char a,c; clrscr(); fp1=fopen("a.txt","r"); fp2=fopen("b.txt","w"); a=getc(fp1); while(!feof(fp1)) { c=fgetc(fp1); if(c==a) { a=c; i++; } else { fprintf(fp2,"%d%c",i,a); a=c; i=1; } } printf("successfully compressed"); fclose(fp1); fclose(fp2); getch(); }

INPUT FILE: a.txt ______ aaabb b.txt _____

3a2b OUTPUT: successfully compressed RESULT: Thus the above program performed successfully and the output is verified.

Ex no:2

IMPLEMENTATION OF TEXT COMPRESSION USING PACKET TEXT METHOD

AIM: To write a program to perform text compression algorithm using packet text method in C language. ALGORITHM: Start the program. Write some text in the input file Using the text in input file & compress the content using packet text method. Write the compressed text (i.e.) translated into another form into output file. Stop the execution. PROGRAM
#include <stdio.h> #include <string.h> #include <stdbool.h> #define RESULTSTRINGMAXLENGTH 255 int packtext(char *text, const bool compress); int main(void) { int original; char text[]="Quidquid latine dictum sit, altum viditur."; original=strlen(text); printf("%s length=%ld\n",text,strlen(text)); packtext(text,true); printf("compressed length=%ld\nCompression Ratio=%.0f%%\n",strlen(text),(float)strlen(text)/original*100); packtext(text,false); printf("String Restored: %s length=%ld",text,strlen(text)); return 0; } int packtext(char *text, const bool compress) { const char dictionary[]=" e as tinthouerhet anreesr d onn or o i y wo tontyo. neisarteed, ctiy bat snd fal pensestvengitu talehaurllcousa mf dfoof siril hmeg om Icehironsasiossbedepe rli Tetel nicho lilprcactutThpaeceachh wige ebuaisursulmawaotowtsmploI solyee Cunm rtieno Sdiwhs.rafincademe.irplk ury Pwoacos gams,duayavucColamowe Aoopu"; int counter,resultcounter=0; if (strlen(dictionary) != 320) { printf("ERROR: Dictionary has the wrong size\n");

return 2; } if (compress) { int dictsearch; for (counter=0; counter < strlen(text); counter++) if (text[counter] < 32 || text[counter]==127) { printf("ERROR: String contains characters with values out of range"); return 1; } for (counter=0; counter < strlen(text); counter++, resultcounter++) { for (dictsearch=0; dictsearch < strlen(dictionary); dictsearch+=2) if (text[counter]==dictionary[dictsearch] && text[counter+1]==dictionary[dictsearch+1]) break; if (dictsearch < strlen(dictionary)) // we found the characters that we want { text[resultcounter]=dictsearch/2+96; counter++; } else text[resultcounter]=text[counter]-31; } text[resultcounter]='\0'; } else //uncompress { char result[RESULTSTRINGMAXLENGTH]; for (counter=0; counter < strlen(text); counter++,resultcounter++) { if (text[counter]<96 && text[counter] > 0) result[resultcounter]=text[counter]+31; else { if (text[counter]>95) { result[resultcounter]=dictionary[((int)text[counter]-96)*2]; resultcounter++; result[resultcounter]=dictionary[((int)text[counter]-96)*2+1]; } else { result[resultcounter]=dictionary[(160+text[counter])*2]; resultcounter++; result[resultcounter]=dictionary[(160+text[counter])*2+1]; }

} } result[resultcounter]='\0'; strcpy(text,result); } return 0; }

INPUT FILE: a.txt ______ aaabb b.txt _____

3a2b OUTPUT: successfully compressed RESULT: Thus the above program performed successfully and the output is verified.

Vous aimerez peut-être aussi