Remove consecutive duplicate characters in a string

Here given code implementation process.

//C Program
//Remove consecutive duplicate characters in a string
#include <stdio.h>

//Remove all consecutive repeating characters from the string
void remove_consecutive(char * text, int size)
{
	if (size <= 0)
	{
		return;
	}
	printf("\nBefore : %s", text);
	int index = 0;
	int i = 1;
	for (i = 1; i < size; ++i)
	{
		if (text[i] != text[index])
		{
			// When are no consecutive repeating characters
			index++;
			text[index] = text[i];
		}
	}
	if (index != 0 && (index + 1) < size)
	{
		// When needed to add a null character
		text[index + 1] = '\0';
	}
	printf("\n");
}
int main()
{
	//Define the character elements
	char text[] = "codexxxyyyzzzzz";
	//Get the size
	int size = sizeof(text) / sizeof(text[0]) - 1;
	remove_consecutive(text, size);
	printf("After : %s", text);
	// Other cases
	char text1[] = "wwwrronnngcooooooodeeteeest";
	size = sizeof(text1) / sizeof(text1[0]) - 1;
	remove_consecutive(text1, size);
	printf("After : %s", text1);
	return 0;
}

Output

Before : codexxxyyyzzzzz
After : codexyz
Before : wwwrronnngcooooooodeeteeest
After : wrongcodetest
//Java Program 
//Remove consecutive duplicate characters in a string
class MyString
{
	//Remove all consecutive repeating characters from the string
	public String remove_consecutive(String text)
	{
		int size = text.length();
		if (size <= 0)
		{
			return "";
		}
		System.out.print("\n Before : " + text + "");
		int i = 0;
		String result = "";
		for (i = 0; i < size; ++i)
		{
			if (result.length() == 0 || (text.charAt(i) != result.charAt(result.length() - 1)))
			{
				// When are no consecutive repeating characters
				result += text.charAt(i);
			}
		}
		return result;
	}
	public static void main(String[] args)
	{
		MyString obj = new MyString();
		String text = "codexxxyyyzzzzz";
		text = obj.remove_consecutive(text);
		System.out.print("\n After Result : " + text + "\n");
		text = "wwwrronnngcooooooodeeteeest";
		text = obj.remove_consecutive(text);
		System.out.print("\n After Result : " + text + "\n");
	}
}

Output

 Before : codexxxyyyzzzzz
 After Result : codexyz

 Before : wwwrronnngcooooooodeeteeest
 After Result : wrongcodetest
//Include header file
#include <iostream>

#include<string.h>

using namespace std;
//C++ Program 
//Remove consecutive duplicate characters in a string
class MyString
{
	public:
		//Remove all consecutive repeating characters from the string
		string remove_consecutive(string text)
		{
			int size = text.size();
			if (size <= 0)
			{
				return "";
			}
			cout << "\n Before : " << text << "";
			int i = 0;
			string result = "";
			for (i = 0; i < size; ++i)
			{
				if (result.size() == 0 || (text[i] != result[result.size() - 1]))
				{
					// When are no consecutive repeating characters
					result += text[i];
				}
			}
			return result;
		}
};
int main()
{
	MyString obj = MyString();
	string text = "codexxxyyyzzzzz";
	text = obj.remove_consecutive(text);
	cout << "\n After Result : " << text << "\n";
	text = "wwwrronnngcooooooodeeteeest";
	text = obj.remove_consecutive(text);
	cout << "\n After Result : " << text << "\n";
	return 0;
}

Output

 Before : codexxxyyyzzzzz
 After Result : codexyz

 Before : wwwrronnngcooooooodeeteeest
 After Result : wrongcodetest
//Include namespace system
using System;
//C# Program 
//Remove consecutive duplicate characters in a string
class MyString
{
	//Remove all consecutive repeating characters from the string
	public String remove_consecutive(String text)
	{
		int size = text.Length;
		if (size <= 0)
		{
			return "";
		}
		Console.Write("\n Before : " + text + "");
		int i = 0;
		String result = "";
		for (i = 0; i < size; ++i)
		{
			if (result.Length == 0 || (text[i] != result[result.Length - 1]))
			{
				// When are no consecutive repeating characters
				result += text[i];
			}
		}
		return result;
	}
	public static void Main(String[] args)
	{
		MyString obj = new MyString();
		String text = "codexxxyyyzzzzz";
		text = obj.remove_consecutive(text);
		Console.Write("\n After Result : " + text + "\n");
		text = "wwwrronnngcooooooodeeteeest";
		text = obj.remove_consecutive(text);
		Console.Write("\n After Result : " + text + "\n");
	}
}

Output

 Before : codexxxyyyzzzzz
 After Result : codexyz

 Before : wwwrronnngcooooooodeeteeest
 After Result : wrongcodetest
<?php
//Php Program 
//Remove consecutive duplicate characters in a string
class MyString
{
	//Remove all consecutive repeating characters from the string
	public	function remove_consecutive($text)
	{
		$size = strlen($text);
		if ($size <= 0)
		{
			return "";
		}
		echo "\n Before : ". $text ."";
		$i = 0;
		$result = "";
		for ($i = 0; $i < $size; ++$i)
		{
			if (strlen($result) == 0 || ($text[$i] != $result[strlen($result) - 1]))
			{
				// When are no consecutive repeating characters
				$result .= $text[$i];
			}
		}
		return $result;
	}
}

function main()
{
	$obj = new MyString();
	$text = "codexxxyyyzzzzz";
	$text = $obj->remove_consecutive($text);
	echo "\n After Result : ". $text ."\n";
	$text = "wwwrronnngcooooooodeeteeest";
	$text = $obj->remove_consecutive($text);
	echo "\n After Result : ". $text ."\n";
}
main();

Output

 Before : codexxxyyyzzzzz
 After Result : codexyz

 Before : wwwrronnngcooooooodeeteeest
 After Result : wrongcodetest
//Node Js Program 
//Remove consecutive duplicate characters in a string
class MyString
{
	//Remove all consecutive repeating characters from the string
	remove_consecutive(text)
	{
		var size = text.length;
		if (size <= 0)
		{
			return "";
		}
		process.stdout.write("\n Before : " + text + "");
		var i = 0;
		var result = "";
		for (i = 0; i < size; ++i)
		{
			if (result.length == 0 || (text[i] != result[result.length - 1]))
			{
				// When are no consecutive repeating characters
				result += text[i];
			}
		}
		return result;
	}
}

function main()
{
	var obj = new MyString();
	var text = "codexxxyyyzzzzz";
	text = obj.remove_consecutive(text);
	process.stdout.write("\n After Result : " + text + "\n");
	text = "wwwrronnngcooooooodeeteeest";
	text = obj.remove_consecutive(text);
	process.stdout.write("\n After Result : " + text + "\n");
}
main();

Output

 Before : codexxxyyyzzzzz
 After Result : codexyz

 Before : wwwrronnngcooooooodeeteeest
 After Result : wrongcodetest
# Python 3 Program 
# Remove consecutive duplicate characters in a string
class MyString :
	# Remove all consecutive repeating characters from the string
	def remove_consecutive(self, text) :
		size = len(text)
		if (size <= 0) :
			return ""
		
		print("\n Before : ", text ,"", end = "")
		i = 0
		result = ""
		while (i < size) :
			if (len(result) == 0 or(text[i] != result[len(result) - 1])) :
				#  When are no consecutive repeating characters
				result += text[i]
			
			i += 1
		
		return result
	

def main() :
	obj = MyString()
	text = "codexxxyyyzzzzz"
	text = obj.remove_consecutive(text)
	print("\n After Result : ", text ,"\n", end = "")
	text = "wwwrronnngcooooooodeeteeest"
	text = obj.remove_consecutive(text)
	print("\n After Result : ", text ,"\n", end = "")

if __name__ == "__main__": main()

Output

 Before :  codexxxyyyzzzzz
 After Result :  codexyz

 Before :  wwwrronnngcooooooodeeteeest
 After Result :  wrongcodetest
# Ruby Program 
# Remove consecutive duplicate characters in a string
class MyString

	# Remove all consecutive repeating characters from the string
	def remove_consecutive(text)
	
		size = text.length()
		if (size <= 0)
		
			return ""
		end
		print("\n Before : ", text ,"")
		i = 0
		result = ""
		while (i < size)
		
			if (result.length() == 0 || (text[i] != result[result.length() - 1]))
			
				#  When are no consecutive repeating characters
				result += text[i]
			end
			i += 1
		end
		return result
	end
end
def main()

	obj = MyString.new()
	text = "codexxxyyyzzzzz"
	text = obj.remove_consecutive(text)
	print("\n After Result : ", text ,"\n")
	text = "wwwrronnngcooooooodeeteeest"
	text = obj.remove_consecutive(text)
	print("\n After Result : ", text ,"\n")
end
main()

Output

 Before : codexxxyyyzzzzz
 After Result : codexyz

 Before : wwwrronnngcooooooodeeteeest
 After Result : wrongcodetest
//Scala Program 
//Remove consecutive duplicate characters in a string
class MyString
{
	//Remove all consecutive repeating characters from the string
	def remove_consecutive(text: String): String = {
		var size: Int = text.length();
		if (size <= 0)
		{
			return "";
		}
		print("\n Before : " + text + "");
		var i: Int = 0;
		var result: String = "";
		while (i < size)
		{
			if (result.length() == 0 || (text(i) != result(result.length() - 1)))
			{
				// When are no consecutive repeating characters
				result += text(i);
			}
			i += 1;
		}
		return result;
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var obj: MyString = new MyString();
		var text: String = "codexxxyyyzzzzz";
		text = obj.remove_consecutive(text);
		print("\n After Result : " + text + "\n");
		text = "wwwrronnngcooooooodeeteeest";
		text = obj.remove_consecutive(text);
		print("\n After Result : " + text + "\n");
	}
}

Output

 Before : codexxxyyyzzzzz
 After Result : codexyz

 Before : wwwrronnngcooooooodeeteeest
 After Result : wrongcodetest
//Swift Program 
//Remove consecutive duplicate characters in a string
class MyString
{
	//Remove all consecutive repeating characters from the string
	func remove_consecutive(_ data: String) -> String
	{
		let size: Int = data.count;
		if (size <= 0)
		{
			return "";
		}
		print("\n Before : ", data );
      	let text = Array(data);
		var i: Int = 1;
		var result: String = String(text[0]);
        var index = 0;
		while (i < size)
		{
			if ( text[i] != text[index])
			{
				// When are no consecutive repeating characters
				result += String(text[i]);
                index = i;
			}
			i += 1;
		}
		return result;
	}
}
func main()
{
	let obj: MyString = MyString();
	var text: String = "codexxxyyyzzzzz";
	text = obj.remove_consecutive(text);
	print("\n After Result : ", text ,"\n", terminator: "");
	text = "wwwrronnngcooooooodeeteeest";
	text = obj.remove_consecutive(text);
	print("\n After Result : ", text ,"\n", terminator: "");
}
main();

Output

 Before :  codexxxyyyzzzzz

 After Result :  codexyz

 Before :  wwwrronnngcooooooodeeteeest

 After Result :  wrongcodetest


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