Skip to main content

Run length encoding

Here given code implementation process.

// Java program for
// Run length encoding
public class Encoding
{
	public void runLengthEncoding(String text)
	{
		int n = text.length();
		if (n == 0)
		{
			return;
		}
		String result = "";
		int i = 0;
		int count = 0;
		while (i < n)
		{
			// Get current character
			char temp = text.charAt(i);
			i++;
			// Set counter
			count = 1;
			while (i < n && temp == text.charAt(i))
			{
				// When adjacent element is similar
				i++;
				count++;
			}
			// Add character and its occurrence
			result = result + temp + "" + count;
		}
		// Display given text
		System.out.println(" Given Text : " + text);
		// Display the encoded running length value
		System.out.println(" Result : " + result);
	}
	public static void main(String[] args)
	{
		Encoding task = new Encoding();
		task.runLengthEncoding("aaaaaiiimmmmmm");
		task.runLengthEncoding("cooool");
	}
}

Output

 Given Text : aaaaaiiimmmmmm
 Result : a5i3m6
 Given Text : cooool
 Result : c1o4l1
// Include header file
#include <iostream>
#include <string>

using namespace std;
// C++ program for
// Run length encoding
class Encoding
{
	public: void runLengthEncoding(string text)
	{
		int n = text.length();
		if (n == 0)
		{
			return;
		}
		string result = "";
		int i = 0;
		int count = 0;
		while (i < n)
		{
			// Get current character
			char temp = text[i];
			i++;
			// Set counter
			count = 1;
			while (i < n && temp == text[i])
			{
				// When adjacent element is similar
				i++;
				count++;
			}
			// Add character and its occurrence
			result = result  +  (temp) +  to_string(count);
		}
		// Display given text
		cout << " Given Text : " << text << endl;
		// Display the encoded running length value
		cout << " Result : " << result << endl;
	}
};
int main()
{
	Encoding *task = new Encoding();
	task->runLengthEncoding("aaaaaiiimmmmmm");
	task->runLengthEncoding("cooool");
	return 0;
}

Output

 Given Text : aaaaaiiimmmmmm
 Result : a5i3m6
 Given Text : cooool
 Result : c1o4l1
// Include namespace system
using System;
// Csharp program for
// Run length encoding
public class Encoding
{
	public void runLengthEncoding(String text)
	{
		int n = text.Length;
		if (n == 0)
		{
			return;
		}
		String result = "";
		int i = 0;
		int count = 0;
		while (i < n)
		{
			// Get current character
			char temp = text[i];
			i++;
			// Set counter
			count = 1;
			while (i < n && temp == text[i])
			{
				// When adjacent element is similar
				i++;
				count++;
			}
			// Add character and its occurrence
			result = result + temp + "" + count;
		}
		// Display given text
		Console.WriteLine(" Given Text : " + text);
		// Display the encoded running length value
		Console.WriteLine(" Result : " + result);
	}
	public static void Main(String[] args)
	{
		Encoding task = new Encoding();
		task.runLengthEncoding("aaaaaiiimmmmmm");
		task.runLengthEncoding("cooool");
	}
}

Output

 Given Text : aaaaaiiimmmmmm
 Result : a5i3m6
 Given Text : cooool
 Result : c1o4l1
package main
import "strconv"
import "fmt"
// Go program for
// Run length encoding

func runLengthEncoding(text string) {
	var n int = len(text)
	if n == 0 {
		return
	}
	var result string = ""
	var i int = 0
	var count int = 0
	for (i < n) {
		// Get current character
		var temp byte = text[i]
		i++
		// Set counter
		count = 1
		for (i < n && temp == text[i]) {
			// When adjacent element is similar
			i++
			count++
		}
		// Add character and its occurrence
		result = result + string(temp) + strconv.Itoa(count)
	}
	// Display given text
	fmt.Println(" Given Text : ", text)
	// Display the encoded running length value
	fmt.Println(" Result : ", result)
}
func main() {

	runLengthEncoding("aaaaaiiimmmmmm")
	runLengthEncoding("cooool")
}

Output

 Given Text : aaaaaiiimmmmmm
 Result : a5i3m6
 Given Text : cooool
 Result : c1o4l1
<?php
// Php program for
// Run length encoding
class Encoding
{
	public	function runLengthEncoding($text)
	{
		$n = strlen($text);
		if ($n == 0)
		{
			return;
		}
		$result = "";
		$i = 0;
		$count = 0;
		while ($i < $n)
		{
			// Get current character
			$temp = $text[$i];
			$i++;
			// Set counter
			$count = 1;
			while ($i < $n && $temp == $text[$i])
			{
				// When adjacent element is similar
				$i++;
				$count++;
			}
			// Add character and its occurrence
			$result = $result.strval($temp).strval($count);
		}
		// Display given text
		echo(" Given Text : ".$text.
			"\n");
		// Display the encoded running length value
		echo(" Result : ".$result.
			"\n");
	}
}

function main()
{
	$task = new Encoding();
	$task->runLengthEncoding("aaaaaiiimmmmmm");
	$task->runLengthEncoding("cooool");
}
main();

Output

 Given Text : aaaaaiiimmmmmm
 Result : a5i3m6
 Given Text : cooool
 Result : c1o4l1
// Node JS program for
// Run length encoding
class Encoding
{
	runLengthEncoding(text)
	{
		var n = text.length;
		if (n == 0)
		{
			return;
		}
		var result = "";
		var i = 0;
		var count = 0;
		while (i < n)
		{
			// Get current character
			var temp = text.charAt(i);
			i++;
			// Set counter
			count = 1;
			while (i < n && temp == text.charAt(i))
			{
				// When adjacent element is similar
				i++;
				count++;
			}
			// Add character and its occurrence
			result = result + temp + "" + count;
		}
		// Display given text
		console.log(" Given Text : " + text);
		// Display the encoded running length value
		console.log(" Result : " + result);
	}
}

function main()
{
	var task = new Encoding();
	task.runLengthEncoding("aaaaaiiimmmmmm");
	task.runLengthEncoding("cooool");
}
main();

Output

 Given Text : aaaaaiiimmmmmm
 Result : a5i3m6
 Given Text : cooool
 Result : c1o4l1
#  Python 3 program for
#  Run length encoding
class Encoding :
	def runLengthEncoding(self, text) :
		n = len(text)
		if (n == 0) :
			return
		
		result = ""
		i = 0
		count = 0
		while (i < n) :
			#  Get current character
			temp = text[i]
			i += 1
			#  Set counter
			count = 1
			while (i < n and temp == text[i]) :
				#  When adjacent element is similar
				i += 1
				count += 1
			
			#  Add character and its occurrence
			result = result + str(temp) + str(count)
		
		#  Display given text
		print(" Given Text : ", text)
		#  Display the encoded running length value
		print(" Result : ", result)
	

def main() :
	task = Encoding()
	task.runLengthEncoding("aaaaaiiimmmmmm")
	task.runLengthEncoding("cooool")

if __name__ == "__main__": main()

Output

 Given Text :  aaaaaiiimmmmmm
 Result :  a5i3m6
 Given Text :  cooool
 Result :  c1o4l1
#  Ruby program for
#  Run length encoding
class Encodings
	def runLengthEncoding(text) 
		n = text.length
		if (n == 0) 
			return
		end

		result = ""
		i = 0
		count = 0
		while (i < n) 
			#  Get current character
			temp = text[i]
			i += 1
			#  Set counter
			count = 1
			while (i < n && temp == text[i]) 
				#  When adjacent element is similar
				i += 1
				count += 1
			end

			#  Add character and its occurrence
			result = result + temp.to_s + count.to_s
		end

		#  Display given text
		print(" Given Text : ", text, "\n")
		#  Display the encoded running length value
		print(" Result : ", result, "\n")
	end

end

def main() 
	task = Encodings.new()
	task.runLengthEncoding("aaaaaiiimmmmmm")
	task.runLengthEncoding("cooool")
end

main()

Output

 Given Text : aaaaaiiimmmmmm
 Result : a5i3m6
 Given Text : cooool
 Result : c1o4l1
// Scala program for
// Run length encoding
class Encoding()
{
	def runLengthEncoding(text: String): Unit = {
		var n: Int = text.length();
		if (n == 0)
		{
			return;
		}
		var result: String = "";
		var i: Int = 0;
		var count: Int = 0;
		while (i < n)
		{
			// Get current character
			var temp: Char = text.charAt(i);
			i += 1;
			// Set counter
			count = 1;
			while (i < n && temp == text.charAt(i))
			{
				// When adjacent element is similar
				i += 1;
				count += 1;
			}
			// Add character and its occurrence
			result = result + temp.toString() + "" + count.toString();
		}
		// Display given text
		println(" Given Text : " + text);
		// Display the encoded running length value
		println(" Result : " + result);
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Encoding = new Encoding();
		task.runLengthEncoding("aaaaaiiimmmmmm");
		task.runLengthEncoding("cooool");
	}
}

Output

 Given Text : aaaaaiiimmmmmm
 Result : a5i3m6
 Given Text : cooool
 Result : c1o4l1
import Foundation;
// Swift 4 program for
// Run length encoding
class Encoding
{
	func runLengthEncoding(_ data: String)
	{
      	let text = Array(data);
		let n: Int = text.count;
		if (n == 0)
		{
			return;
		}
		var result: String = "";
		var i: Int = 0;
		var count: Int = 0;
		while (i < n)
		{
			// Get current character
			let temp: Character = text[i];
			i += 1;
			// Set counter
			count = 1;
			while (i < n && temp == text[i])
			{
				// When adjacent element is similar
				i += 1;
				count += 1;
			}
			// Add character and its occurrence
			result = result + String(temp) + "" + String(count);
		}
		// Display given text
		print(" Given Text : ", data);
		// Display the encoded running length value
		print(" Result : ", result);
	}
}
func main()
{
	let task: Encoding = Encoding();
	task.runLengthEncoding("aaaaaiiimmmmmm");
	task.runLengthEncoding("cooool");
}
main();

Output

 Given Text :  aaaaaiiimmmmmm
 Result :  a5i3m6
 Given Text :  cooool
 Result :  c1o4l1
// Kotlin program for
// Run length encoding
class Encoding
{
	fun runLengthEncoding(text: String): Unit
	{
		val n: Int = text.length;
		if (n == 0)
		{
			return;
		}
		var result: String = "";
		var i: Int = 0;
		var count: Int ;
		while (i < n)
		{
			// Get current character
			val temp: Char = text.get(i);
			i += 1;
			// Set counter
			count = 1;
			while (i < n && temp == text.get(i))
			{
				// When adjacent element is similar
				i += 1;
				count += 1;
			}
			// Add character and its occurrence
			result = result + temp.toString() + "" + count.toString();
		}
		// Display given text
		println(" Given Text : " + text);
		// Display the encoded running length value
		println(" Result : " + result);
	}
}
fun main(args: Array < String > ): Unit
{
	val task: Encoding = Encoding();
	task.runLengthEncoding("aaaaaiiimmmmmm");
	task.runLengthEncoding("cooool");
}

Output

 Given Text : aaaaaiiimmmmmm
 Result : a5i3m6
 Given Text : cooool
 Result : c1o4l1




Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment