-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompressor.c
More file actions
74 lines (66 loc) · 1.45 KB
/
Copy pathcompressor.c
File metadata and controls
74 lines (66 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void compress(){
int seen_char = getchar();
int next_char;
int repeat_counter = 1;
if (seen_char == EOF) return;
while ((next_char = getchar()) != EOF)
{
if (next_char == seen_char)
{
repeat_counter++;
if (repeat_counter==255)
{
putchar(seen_char);
putchar(repeat_counter);
repeat_counter=0;
}
}
else
{
putchar(seen_char);
putchar(repeat_counter);
repeat_counter=1;
seen_char = next_char;
}
}
putchar(seen_char);
putchar(repeat_counter);
}
void decompress(){
while (1)
{
int c = getchar();
if(c == EOF)
break;
int count = getchar();
if(count == EOF)
break;
for (int i = 0; i < count; i++)
{
putchar(c);
}
}
}
int main(int argc, char *argv[]){
if (argc != 2)
{
printf("Usages: %s [compress|decompress]\n", argv[0]);
exit(-2);
}
if (!strcmp(argv[1] , "compress")) //value in a condition normally must be true to enter the condition
{
compress();
}
else if (!strcmp(argv[1] , "decompress"))
{
decompress();
}
else
{
printf("Usages: %s [compress|decompress]\n", argv[0]);
exit(-2);
}
}