Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Problem (in C language, submit a .c file) 1. Your program needs to compress a te

ID: 3812057 • Letter: P

Question

Problem (in C language, submit a .c file)
1. Your program needs to compress a text file. The file text.txt contains the text to be
compressed and this file contains lower case letters only. The file alpha.txt contains
the code for each letter.


2. You need to create a file called out.txt to store the compressed text.


3. It is sure that all the letters in text.txt will have one and only one corresponding code
in the file alpha.txt.


4. Your program needs to achieve the indeed compression and could solve the "tail bit
problem". You need to submit a one-page document to introduce how these two
problems are solved. To solve the "tail bit problem", you may write some additional
information into the output file.


Sample Input & Output
The content of text.txt is
adbb
The content of alpha.txt is
a 00
b 01
c 10
d 11
The content of out.txt is
5

Explanation / Answer

#include<stdio.h>
#include<string.h>

void main()
{
int i=0,j=0,cnt=0;
char ch, strng[32000];
FILE *f1,*f2,*f3;
  
f1=fopen("text.txt","r");
while((strng[i]=getc(f1))!=EOF)
{
printf("%c",strng[i]);
i++;
}
printf(" ");
  
f3=fopen("out.txt","w");
fclose(f3);
while(j<=i)
{
ch=strng[j];
while(ch==strng[j]&&j<=i)
{
cnt++;
j++;
}
f3=fopen("out.txt","w");
printf(" %c%d",ch,cnt);
fprintf(f3,"%c%d",ch,cnt);
fclose(f3);
cnt=0;
j++;
}
fclose(f1);
  
f2=fopen("alpha.txt","r");
f3=fopen("out.txt","a+");
while((ch=getc(f2))!=EOF)
{
fputc(ch, f3);
}
fclose(f3);
  
getchar();
}

-----------------------------------------

text.txt

abdd

------------------------------------------

alpha.txt

a 00
b 01
c 10
d 11

------------------------------------------------

In out.txt file, the output is written and it is compressed. The alpha.txt file content is append to the out.txt file.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote