Run Length Encoding | CPP program to implement run length encoding ; Count occurrences of current character ; Print character and its count ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printRLE ( string str ) { int n = str . length ( ) ; for ( int i = 0 ; i < n ; i ++ ) { int count = 1 ; while ( i < n - 1 && str [ i ] == str [ i + 1 ] ) { count ++ ; i ++ ; } cout << str [ i ] << count ; } } int main ( ) { string str = " wwwwaaadexxxxxxywww " ; printRLE ( str ) ; return 0 ; }