Answers for "RLE Encoding/Compression c++"

C++
0

RLE Encoding/Compression c++

#include <iostream>
#include <string>
using namespace std;
 
// Perform Run Length Encoding (RLE) data compression algorithm
// on string str
string encode(string str)
{
    // stores output string
    string encoding = "";
    int count;
 
    for (int i = 0; str[i]; i++)
    {
        // count occurrences of character at index i
        count = 1;
        while (str[i] == str[i + 1])
            count++, i++;
 
        // append current character and its count to the result
        encoding += to_string(count) + str[i];
    }
 
    return encoding;
}
 
int main()
{
    string str = "ABBCCCD";
 
    cout << encode(str);
 
    return 0;
}
Posted by: Guest on June-29-2020

Browse Popular Code Answers by Language