How to find Run Length Encoding in C++

In this tutorial, we will learn about the Run Length Encoding (RLE) with example and C++ program followed by the algorithm.

Run Length Encoding is a very simple form of lossless data compression in which repeated or runs of data are stored as a single data value and count.

Run Length Encoding is used in almost all areas especially in movie making for video compression to decrease the size of video without disturbing the quality.



saparating each group of alphabets/symbol with counting: 

1st symbol C = 1             6th symbol P = 1
2nd symbol O = 3             7th symbol E = 2
3rd symbol D = 3             8th symbol D = 7
4th symbol E = 4             9th symbol Y = 9
5th symbol S = 1                    

Therefore, Run length Encoding of given code will be: C1O3D3E4S1P1E2D7Y9

Put all the elements in the array and traverse the array. For each present element print that element and count one, check whether the next element is the same as the present element if so, then increase count by one. else repeat the whole process till the size of the array.  


  1. Declare and initialize a string consider “code”.
  2. start a for loop form i = 0 to the length of string.
    a) Declare and Initialize a variable Count with 1 inside the loop.
    b)  start a loop for every ith element of the string if code[i] is equal to code[i + 1] and ‘i’ is less than the length of code then increase “count” and ‘i’ by one.
    c) print “code[i]” and count.

C++ program to find Run line encoding

#include <cstdlib>
#include <string.h>

using namespace std;

int main(int argc, char** argv) {
    string code;
    cout << "Enter the code: ";
    cin >> code;
    for( int i = 0; i < code.length(); i++){
        int count = 1;
        while(code[i] == code[i + 1] && i < code.length() - 1){
        cout<<"Run Length Encoding is: <<code[i]<<count;
    return 0;


Run Length Encoding is: C1O3D3E4S1P1E2D7Y9

You may also read:
1. Find Hamming distance between two strings in C++

2. Reversal algorithm for array rotation in C++

Leave a Reply

Your email address will not be published. Required fields are marked *