Home » Blog » Write a program for Run Length Encoding

Write a program for Run Length Encoding

Run Length Encoding is a form of lossless data compression in which runs of data are stored as a single data value and count, rather than as the original run.

import java.util.*;

class RLEChallange
{
	public static void main(String args[])
	{
		Scanner sc=new Scanner(System.in);
		String strin,strout="";
		int count,i,j;
		
		System.out.println("Enter String ");
		strin=sc.nextLine();
		
		i=0;
		j=0;
		while(i<strin.length())
		{
			count=1;
			j=i+1;
			while(j<strin.length() && strin.toLowerCase().charAt(i)==strin.toLowerCase().charAt(j))
			{
				count++;
				j++;
			}
			strout=strout+count+strin.charAt(i);
			i=j;
		}
		
		
		System.out.println("\n\nInPut "+strin);
		System.out.println("OutPut "+strout);
	}
}

  • Output
Enter String
AABCCAAA


InPut AABCCAAA
OutPut 2A1B2C3A

Enter String
AAAVVVTYUUUIIIII


InPut AAAVVVTYUUUIIIII
OutPut 3A3V1T1Y3U5I

 

Leave a Reply

Your email address will not be published.