rle encoding 2fcompression c 2b 2b

Solutions on MaxInterview for rle encoding 2fcompression c 2b 2b by the best coders in the world

showing results for - "rle encoding 2fcompression c 2b 2b"
Valentina
11 Oct 2020
1#include <iostream>
2#include <string>
3using namespace std;
4 
5// Perform Run Length Encoding (RLE) data compression algorithm
6// on string str
7string encode(string str)
8{
9    // stores output string
10    string encoding = "";
11    int count;
12 
13    for (int i = 0; str[i]; i++)
14    {
15        // count occurrences of character at index i
16        count = 1;
17        while (str[i] == str[i + 1])
18            count++, i++;
19 
20        // append current character and its count to the result
21        encoding += to_string(count) + str[i];
22    }
23 
24    return encoding;
25}
26 
27int main()
28{
29    string str = "ABBCCCD";
30 
31    cout << encode(str);
32 
33    return 0;
34}
35