Implementing Run Length Encoding in Java

Run-Length Encoding (RLE) is a basic form of data compression where consecutive characters (runs) are replaced with a single character followed by the count of repetitions. This simple technique is useful for compressing repetitive data.

This Java program demonstrates how to apply RLE to a line of text input by the user.

package rle;
import java.io.*;

public class RLE {
    public static void main(String[] args) throws IOException {
        // Create BufferedReader to read input from the console
        BufferedReader obj = new BufferedReader(new InputStreamReader(System.in));

        // Prompt user for input
        System.out.println("Enter line to encode:");
        String ip = obj.readLine();  // Read the input line
        int len = ip.length();       // Store the length of the input string

        int i = 0;                   // Iterator
        int c = 0;                   // Counter for repeated characters
        char f = ip.charAt(0);       // Initialize with first character
        String op = "";              // Output string for encoded data

        // Loop through each character in the input string
        for (; i < len; i++) {
            if (i + 1 < len) { // Check if next character exists
                if (ip.charAt(i) == ip.charAt(i + 1)) {
                    // If current and next character are same, increment count
                    c++;
                } else {
                    // If character changes, append count and character to output
                    op = op + Integer.toHexString(c + 1) + f;
                    c = 0;
                    f = ip.charAt(i + 1); // Update next character
                }
            } else {
                // Last character case: add final count and character
                op = op + Integer.toHexString(c + 1) + f;
            }
        }

        // Output the encoded result and stats
        System.out.println("Encoded line is: " + op);
        System.out.println("Length of original string: " + len);
        System.out.println("Length of encoded string: " + op.length());
        System.out.println("Compression ratio: " + (op.length() * 1.0 / len));
    }
}

Understanding the Code

Key Components:

  • BufferedReader is used to read the input string.
  • Loop through characters: The loop checks each character and compares it to the next one.
  • Counting repetitions: If characters match, the count increases. When a new character appears, it stores the count (in hexadecimal) and character into the encoded string.
  • Output: The encoded string, original length, encoded length, and compression ratio are printed.

Hexadecimal Encoding:

  • The count of repeated characters is encoded in hexadecimal format using Integer.toHexString().

Output Example

Enter line to encode:
11100000000000111111111110000010101111
Encoded line is: 31b0b1501110111041
Length of original string: 38
Length of encoded string: 18
Compression ratio: 0.47368421052631576

Output Explanation

  • The input has repeating sequences such as 111, 000000000000, 1111111111, and so on.
  • For 111, it gets encoded as 31 (3 in hex followed by 1).
  • This process repeats for all sequences, greatly reducing the length.
  • Original Length: 38
  • Encoded Length: 18
  • Compression Ratio: 18 / 38 ≈ 0.47 (about 47% of original size)

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.