-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathRLE.cpp
More file actions
68 lines (61 loc) · 911 Bytes
/
RLE.cpp
File metadata and controls
68 lines (61 loc) · 911 Bytes
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
#include <stdio.h>
#include <string.h>
#define SIZE 1000
void encode(char S[], char E[])
{
int l = strlen(S);
int c = 1;
int k = 0;
for(int i=0; i<l; ++i) {
if(i+1 < l && S[i] == S[i+1]) {
c = c + 1;
} else {
if(c > 1) {
int r = 0;
while(c) {
r = r*10 + c%10;
c = c/10;
} while(r) {
E[k++] = r%10 + '0';
r = r/10;
}
}
E[k++] = S[i];
c = 1;
}
}
E[k] = 0;
}
void decode(char E[], char D[])
{
int l = strlen(E);
int c = 0;
int k = 0;
for(int i=0; i<l; ++i)
{
if('0' <= E[i] && E[i] <= '9') {
c = c*10 + E[i] - '0';
} else {
if(c == 0) {
D[k++] = E[i];
} else {
for(int j=0; j<c; ++j)
D[k++] = E[i];
c = 0;
}
}
}
D[k] = 0;
}
int main(void)
{
char S[SIZE];
char Encode[SIZE];
char Decode[SIZE];
scanf("%s", S);
encode(S, Encode);
puts(Encode);
decode(Encode, Decode);
puts(Decode);
return 0;
}