Remove 3 or more consecutive characters from a string

Here given code implementation process.

/*
    Java Program for
    Remove 3 or more consecutive characters from a string
*/
public class RemoveCharacters
{
	public void remove3orMoreConsecutive(String text)
	{
		// Get the length
		int n = text.length();
		if (n == 0)
		{
			return;
		}
		String result = "";
		int i = 0;
		int k = 0;
		while (i < n)
		{
			while (k + i < n && text.charAt(k + i) == text.charAt(i))
			{
				k++;
			}
			if (k >= 3)
			{
				// skip k consecutive characters
				i += k;
			}
			else
			{
				result = result + text.charAt(i);
				i++;
			}
			k = 0;
		}
		System.out.println(" Given text : " + text);
		System.out.println(" Result     : " + result);
	}
	public static void main(String[] args)
	{
		RemoveCharacters task = new RemoveCharacters();
		// Test
		task.remove3orMoreConsecutive("iyyyiiiidxeeeeccaaa");
		task.remove3orMoreConsecutive("piiinnnnkbole");
	}
}

Output

 Given text : iyyyiiiidxeeeeccaaa
 Result     : idxcc
 Given text : piiinnnnkbole
 Result     : pkbole
// Include header file
#include <iostream>
#include <string>

using namespace std;
/*
    C++ Program for
    Remove 3 or more consecutive characters from a string
*/
class RemoveCharacters
{
	public: void remove3orMoreConsecutive(string text)
	{
		// Get the length
		int n = text.length();
		if (n == 0)
		{
			return;
		}
		string result = "";
		int i = 0;
		int k = 0;
		while (i < n)
		{
			while (k + i < n && text[k + i] == text[i])
			{
				k++;
			}
			if (k >= 3)
			{
				// skip k consecutive characters
				i += k;
			}
			else
			{
				result = result  +  (text[i]);
				i++;
			}
			k = 0;
		}
		cout << " Given text : " << text << endl;
		cout << " Result     : " << result << endl;
	}
};
int main()
{
	RemoveCharacters *task = new RemoveCharacters();
	// Test
	task->remove3orMoreConsecutive("iyyyiiiidxeeeeccaaa");
	task->remove3orMoreConsecutive("piiinnnnkbole");
	return 0;
}

Output

 Given text : iyyyiiiidxeeeeccaaa
 Result     : idxcc
 Given text : piiinnnnkbole
 Result     : pkbole
// Include namespace system
using System;
/*
    Csharp Program for
    Remove 3 or more consecutive characters from a string
*/
public class RemoveCharacters
{
	public void remove3orMoreConsecutive(String text)
	{
		// Get the length
		int n = text.Length;
		if (n == 0)
		{
			return;
		}
		String result = "";
		int i = 0;
		int k = 0;
		while (i < n)
		{
			while (k + i < n && text[k + i] == text[i])
			{
				k++;
			}
			if (k >= 3)
			{
				// skip k consecutive characters
				i += k;
			}
			else
			{
				result = result + text[i];
				i++;
			}
			k = 0;
		}
		Console.WriteLine(" Given text : " + text);
		Console.WriteLine(" Result     : " + result);
	}
	public static void Main(String[] args)
	{
		RemoveCharacters task = new RemoveCharacters();
		// Test
		task.remove3orMoreConsecutive("iyyyiiiidxeeeeccaaa");
		task.remove3orMoreConsecutive("piiinnnnkbole");
	}
}

Output

 Given text : iyyyiiiidxeeeeccaaa
 Result     : idxcc
 Given text : piiinnnnkbole
 Result     : pkbole
package main
import "fmt"
/*
    Go Program for
    Remove 3 or more consecutive characters from a string
*/
type RemoveCharacters struct {}
func getRemoveCharacters() * RemoveCharacters {
	var me *RemoveCharacters = &RemoveCharacters {}
	return me
}
func(this RemoveCharacters) remove3orMoreConsecutive(text string) {
	// Get the length
	var n int = len(text)
	if n == 0 {
		return
	}
	var result string = ""
	var i int = 0
	var k int = 0
	for (i < n) {
		for (k + i < n && text[k + i] == text[i]) {
			k++
		}
		if k >= 3 {
			// skip k consecutive characters
			i += k
		} else {
			result = result + string(text[i])
			i++
		}
		k = 0
	}
	fmt.Println(" Given text : ", text)
	fmt.Println(" Result     : ", result)
}
func main() {
	var task * RemoveCharacters = getRemoveCharacters()
	// Test
	task.remove3orMoreConsecutive("iyyyiiiidxeeeeccaaa")
	task.remove3orMoreConsecutive("piiinnnnkbole")
}

Output

 Given text : iyyyiiiidxeeeeccaaa
 Result     : idxcc
 Given text : piiinnnnkbole
 Result     : pkbole
<?php
/*
    Php Program for
    Remove 3 or more consecutive characters from a string
*/
class RemoveCharacters
{
	public	function remove3orMoreConsecutive($text)
	{
		// Get the length
		$n = strlen($text);
		if ($n == 0)
		{
			return;
		}
		$result = "";
		$i = 0;
		$k = 0;
		while ($i < $n)
		{
			while ($k + $i < $n && $text[$k + $i] == $text[$i])
			{
				$k++;
			}
			if ($k >= 3)
			{
				// skip k consecutive characters
				$i += $k;
			}
			else
			{
				$result = $result.strval($text[$i]);
				$i++;
			}
			$k = 0;
		}
		echo(" Given text : ".$text.
			"\n");
		echo(" Result     : ".$result.
			"\n");
	}
}

function main()
{
	$task = new RemoveCharacters();
	// Test
	$task->remove3orMoreConsecutive("iyyyiiiidxeeeeccaaa");
	$task->remove3orMoreConsecutive("piiinnnnkbole");
}
main();

Output

 Given text : iyyyiiiidxeeeeccaaa
 Result     : idxcc
 Given text : piiinnnnkbole
 Result     : pkbole
/*
    Node JS Program for
    Remove 3 or more consecutive characters from a string
*/
class RemoveCharacters
{
	remove3orMoreConsecutive(text)
	{
		// Get the length
		var n = text.length;
		if (n == 0)
		{
			return;
		}
		var result = "";
		var i = 0;
		var k = 0;
		while (i < n)
		{
			while (k + i < n && text.charAt(k + i) == text.charAt(i))
			{
				k++;
			}
			if (k >= 3)
			{
				// skip k consecutive characters
				i += k;
			}
			else
			{
				result = result + text.charAt(i);
				i++;
			}
			k = 0;
		}
		console.log(" Given text : " + text);
		console.log(" Result     : " + result);
	}
}

function main()
{
	var task = new RemoveCharacters();
	// Test
	task.remove3orMoreConsecutive("iyyyiiiidxeeeeccaaa");
	task.remove3orMoreConsecutive("piiinnnnkbole");
}
main();

Output

 Given text : iyyyiiiidxeeeeccaaa
 Result     : idxcc
 Given text : piiinnnnkbole
 Result     : pkbole
#    Python 3 Program for
#    Remove 3 or more consecutive characters from a string
class RemoveCharacters :
	def remove3orMoreConsecutive(self, text) :
		#  Get the length
		n = len(text)
		if (n == 0) :
			return
		
		result = ""
		i = 0
		k = 0
		while (i < n) :
			while (k + i < n and text[k + i] == text[i]) :
				k += 1
			
			if (k >= 3) :
				#  skip k consecutive characters
				i += k
			else :
				result = result + str(text[i])
				i += 1
			
			k = 0
		
		print(" Given text : ", text)
		print(" Result     : ", result)
	

def main() :
	task = RemoveCharacters()
	#  Test
	task.remove3orMoreConsecutive("iyyyiiiidxeeeeccaaa")
	task.remove3orMoreConsecutive("piiinnnnkbole")

if __name__ == "__main__": main()

Output

 Given text :  iyyyiiiidxeeeeccaaa
 Result     :  idxcc
 Given text :  piiinnnnkbole
 Result     :  pkbole
#    Ruby Program for
#    Remove 3 or more consecutive characters from a string
class RemoveCharacters 
	def remove3orMoreConsecutive(text) 
		#  Get the length
		n = text.length
		if (n == 0) 
			return
		end

		result = ""
		i = 0
		k = 0
		while (i < n) 
			while (k + i < n && text[k + i] == text[i]) 
				k += 1
			end

			if (k >= 3) 
				#  skip k consecutive characters
				i += k
			else
 
				result = result + text[i].to_s
				i += 1
			end

			k = 0
		end

		print(" Given text : ", text, "\n")
		print(" Result     : ", result, "\n")
	end

end

def main() 
	task = RemoveCharacters.new()
	#  Test
	task.remove3orMoreConsecutive("iyyyiiiidxeeeeccaaa")
	task.remove3orMoreConsecutive("piiinnnnkbole")
end

main()

Output

 Given text : iyyyiiiidxeeeeccaaa
 Result     : idxcc
 Given text : piiinnnnkbole
 Result     : pkbole
import scala.collection.mutable._;
/*
    Scala Program for
    Remove 3 or more consecutive characters from a string
*/
class RemoveCharacters()
{
	def remove3orMoreConsecutive(text: String): Unit = {
		// Get the length
		var n: Int = text.length();
		if (n == 0)
		{
			return;
		}
		var result: String = "";
		var i: Int = 0;
		var k: Int = 0;
		while (i < n)
		{
			while (k + i < n && text.charAt(k + i) == text.charAt(i))
			{
				k += 1;
			}
			if (k >= 3)
			{
				// skip k consecutive characters
				i += k;
			}
			else
			{
				result = result + text.charAt(i).toString();
				i += 1;
			}
			k = 0;
		}
		println(" Given text : " + text);
		println(" Result     : " + result);
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: RemoveCharacters = new RemoveCharacters();
		// Test
		task.remove3orMoreConsecutive("iyyyiiiidxeeeeccaaa");
		task.remove3orMoreConsecutive("piiinnnnkbole");
	}
}

Output

 Given text : iyyyiiiidxeeeeccaaa
 Result     : idxcc
 Given text : piiinnnnkbole
 Result     : pkbole
import Foundation;
/*
    Swift 4 Program for
    Remove 3 or more consecutive characters from a string
*/
class RemoveCharacters
{
	func remove3orMoreConsecutive(_ data: String)
	{
      	let text = Array(data);
		// Get the length
		let n: Int = text.count;
		if (n == 0)
		{
			return;
		}
		var result: String = "";
		var i: Int = 0;
		var k: Int = 0;
		while (i < n)
		{
			while (k + i < n && text[k + i] == text[i])
			{
				k += 1;
			}
			if (k >= 3)
			{
				// skip k consecutive characters
				i += k;
			}
			else
			{
				result = result + String(text[i]);
				i += 1;
			}
			k = 0;
		}
		print(" Given text : ", data);
		print(" Result     : ", result);
	}
}
func main()
{
	let task: RemoveCharacters = RemoveCharacters();
	// Test
	task.remove3orMoreConsecutive("iyyyiiiidxeeeeccaaa");
	task.remove3orMoreConsecutive("piiinnnnkbole");
}
main();

Output

 Given text :  iyyyiiiidxeeeeccaaa
 Result     :  idxcc
 Given text :  piiinnnnkbole
 Result     :  pkbole
/*
    Kotlin Program for
    Remove 3 or more consecutive characters from a string
*/
class RemoveCharacters
{
	fun remove3orMoreConsecutive(text: String): Unit
	{
		// Get the length
		val n: Int = text.length;
		if (n == 0)
		{
			return;
		}
		var result: String = "";
		var i: Int = 0;
		var k: Int = 0;
		while (i < n)
		{
			while (k + i < n && text.get(k + i) == text.get(i))
			{
				k += 1;
			}
			if (k >= 3)
			{
				// skip k consecutive characters
				i += k;
			}
			else
			{
				result = result + text.get(i).toString();
				i += 1;
			}
			k = 0;
		}
		println(" Given text : " + text);
		println(" Result     : " + result);
	}
}
fun main(args: Array < String > ): Unit
{
	val task: RemoveCharacters = RemoveCharacters();
	// Test
	task.remove3orMoreConsecutive("iyyyiiiidxeeeeccaaa");
	task.remove3orMoreConsecutive("piiinnnnkbole");
}

Output

 Given text : iyyyiiiidxeeeeccaaa
 Result     : idxcc
 Given text : piiinnnnkbole
 Result     : pkbole


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







© 2021, kalkicode.com, All rights reserved