Quick practice writing a run-length encoder/decoder in C
John J. Workman (@workmajj)
I worked on this software during my time as a Recurse Center facilitator. If you'd like to join a warm community of programmers dedicated to self-improvement, you should consider applying. :-)
This pair of programs generates files with runs of capital letters (rungen
), then encodes/decodes them (runlen
) using a simple run-length encoding scheme. The code is robust in some ways (e.g., will skip newlines and other irrelevant chars) but mostly meant as a quick C exercise.
The RLE scheme used places multiplier numbers before their associated letters, and copies single letters verbatim (since, for example, 1X1Y1Z
takes more room than just XYZ
).
-
In the project directory, build the two programs from source:
$ make
-
Next, use
rungen
to create a file of random capital letter runs:$ ./rungen txt/original.txt
-
Now use
runlen encode
to run-length encode that original file:$ ./runlen encode txt/original.txt txt/encoded.txt
-
And
runlen decode
to decode that compressed version:$ ./runlen encode txt/encoded.txt txt/decoded.txt
-
Finally, use
diff
to confirm that the original and decoded files match:$ diff txt/original.txt txt/decoded.txt
This code is in the public domain.