-
Notifications
You must be signed in to change notification settings - Fork 2
/
question8.c
41 lines (35 loc) · 859 Bytes
/
question8.c
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
/*
Run length encoding
Run length encoding means, traversing through the given character array and displaying which character
is repeating how many times along with the character as output.
Eg: SSMMMAAARRT => S2M3A3R2T1
Applications: File compression
METHOD:
Just traverse and maintain a counter to solve the algo
Time complexity: O(n)
Space complexity: O(1)
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void runLengthEncoding(char *arr, int size){
int i,j=0;
// char *finalStr = (char *)malloc(sizeof(char)*(size*2 + 1));
int counter = 1;
printf("%s\n", arr);
for(i=0;i<size;i++){
// finalStr[j++] = arr[i];
if(arr[i] == arr[i+1]){
counter++;
}else{
printf("%c%d", arr[i],counter);
counter=1;
}
}
}
int main(){
char arr[] = "SSMMMAAARRT";
int length = strlen(arr);
runLengthEncoding(arr,length);
return 0;
}