-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunlenencod.cpp
46 lines (45 loc) · 925 Bytes
/
runlenencod.cpp
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
#include<stdio.h>
#include<map>
#include<string.h>
#include<stdlib.h>
using namespace std;
/* Returns the Run Length Encoded string for the
source string src */
char *encode(char *src)
{
char arr[200];
int n = strlen(src);
char *str = (char *) malloc(sizeof(char)*(n*2 + 1));
int i = 0;
int j = 0;
while(i < n)
{
int count = 1;
*(str+j) = *(src+i);
j++;
while(i+1 < n && *(src+i) == *(src+i+1))
{
count++;
i++;
}
sprintf(arr, "%d", count);
int k =0 ;
while(*(arr+k) != '\0')
{
*(str+j) = *(arr+k);
j++;
k++;
}
i++;
}
*(str+j) = '\0';
return str;
}
/*driver program to test above function */
int main()
{
char str[] = "geeksforgeeks";
char *res = encode(str);
printf("%s\n", res);
// getchar();
}