Created
December 8, 2018 13:42
-
-
Save heatblazer/15cb1aea3f3fc8ffdb7330f029cc4bc0 to your computer and use it in GitHub Desktop.
RLE algorithm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
static int codes[0xFF]= {0}; | |
static void addcode(unsigned char c) | |
{ | |
codes[(int)c]++; | |
} | |
char* RLE(const char* str) | |
{ | |
if (!str) | |
return 0; | |
char* data = (char*) malloc(sizeof(char) * 2+1); | |
if (!data) | |
return 0; | |
int i = 0, j = 0; | |
int len = strlen(str); | |
int rLen = 0; | |
for(i=0; i < len; i++) | |
{ | |
data[j++] = str[i]; | |
for(const char* it = &str[i]; *it == str[i]; it++) | |
{ | |
rLen++; | |
} | |
i += rLen-1; | |
char buff[5] = {0}; // print buffer | |
sprintf(buff, "%d", rLen); | |
strcat(&data[j], buff); | |
j += strlen(buff); | |
rLen = 0; | |
} | |
return data; | |
} | |
int main() | |
{ | |
char* d = RLE("aaaaaaaaaaaaaaaaaaasssssssssssssssssssssbbbbbbbbbbbbbbbbbbbbsssssssssssssssssssssasasas"); | |
printf("%s\r\n", d); | |
free(d); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment