Run Length Encoding | Java program to implement run length encoding ; Count occurrences of current character ; Print character and its count ; Driver code
public class RunLength_Encoding { public static void printRLE ( String str ) { int n = str . length ( ) ; int count = 1 ; for ( int i = 1 ; i < n ; i ++ ) { if ( str . charAt ( i ) == str . charAt ( i - 1 ) ) { count ++ } else { System . out . print ( str . charAt ( i ) ) ; System . out . print ( count ) ; count = 1 } System . out . print ( str . charAt ( i ) ) ; System . out . print ( count ) ; } } public static void main ( String [ ] args ) { String str = " wwwwaaadexxxxxxywww " ; printRLE ( str ) ; } }