Algorithms_in_C 1.0.0
Set of algorithms implemented in C.
Loading...
Searching...
No Matches
run_length_encoding.c File Reference

Encode a null terminated string using Run-length encoding More...

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
Include dependency graph for run_length_encoding.c:

Functions

char * run_length_encode (char *str)
 for IO operations More...
 
static void test ()
 Self-test implementations. More...
 
int main ()
 Main function. More...
 

Detailed Description

Encode a null terminated string using Run-length encoding

Author
serturx

Run-length encoding is a lossless compression algorithm. It works by counting the consecutive occurences symbols and encodes that series of consecutive symbols into the counted symbol and a number denoting the number of consecutive occorences.

For example the string "AAAABBCCD" gets encoded into "4A2B2C1D"

Function Documentation

◆ main()

int main ( void  )

Main function.

Returns
0 on exit
88 {
89 test(); // run self-test implementations
90 printf("All tests have passed!\n");
91 return 0;
92}
static void test()
Self-test implementations.
Definition: run_length_encoding.c:71
Here is the call graph for this function:

◆ run_length_encode()

char * run_length_encode ( char *  str)

for IO operations

for string functions for malloc/free for assert

Encodes a null-terminated string using run-length encoding

Parameters
strString to encode
Returns
char* Encoded string
27 {
28 int str_length = strlen(str);
29 int encoded_index = 0;
30
31 //allocate space for worst-case scenario
32 char* encoded = malloc(2 * strlen(str));
33
34 //temp space for int to str conversion
35 char int_str[20];
36
37 for(int i = 0; i < str_length; ++i) {
38 int count = 0;
39 char current = str[i];
40
41 //count occurences
42 while(current == str[i + count]) count++;
43
44 i += count - 1;
45
46 //convert occurrence amount to string and write to encoded string
47 sprintf(int_str, "%d", count);
48 int int_str_length = strlen(int_str);
49 strncpy(&encoded[encoded_index], int_str, strlen(int_str));
50
51 //write current char to encoded string
52 encoded_index += strlen(int_str);
53 encoded[encoded_index] = current;
54 ++encoded_index;
55 }
56
57 //null terminate string and move encoded string to compacted memory space
58 encoded[encoded_index] = '\0';
59 char* compacted_string = malloc(strlen(encoded) + 1);
60 strcpy(compacted_string, encoded);
61
62 free(encoded);
63
64 return compacted_string;
65}
#define malloc(bytes)
This macro replace the standard malloc function with malloc_dbg.
Definition: malloc_dbg.h:18
#define free(ptr)
This macro replace the standard free function with free_dbg.
Definition: malloc_dbg.h:26

◆ test()

static void test ( void  )
static

Self-test implementations.

Returns
void
71 {
72 char* test;
73 test = run_length_encode("aaaaaaabbbaaccccdefaadr");
74 assert(!strcmp(test, "7a3b2a4c1d1e1f2a1d1r"));
75 free(test);
76 test = run_length_encode("lidjhvipdurevbeirbgipeahapoeuhwaipefupwieofb");
77 assert(!strcmp(test, "1l1i1d1j1h1v1i1p1d1u1r1e1v1b1e1i1r1b1g1i1p1e1a1h1a1p1o1e1u1h1w1a1i1p1e1f1u1p1w1i1e1o1f1bq"));
78 free(test);
79 test = run_length_encode("htuuuurwuquququuuaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaahghghrw");
80 assert(!strcmp(test, "1h1t4u1r1w1u1q1u1q1u1q3u76a1h1g1h1g1h1r1w"));
81 free(test);
82}
char * run_length_encode(char *str)
for IO operations
Definition: run_length_encoding.c:27
Here is the call graph for this function: