Find 1s complement of a binary string

Here given code implementation process.

/*
    C program for
    Find 1s complement of a binary string
*/
// Include header file
#include <stdio.h>

#include <string.h>

void oneScomplement(char data[])
{
	// Get the number of character in given data
	int n = strlen(data);
	printf(" Given Binary   : %s", data);
	// Change binary string into 1's complement
	for (int i = 0; i < n; ++i)
	{
		if (data[i] == '0')
		{
			data[i] = '1';
		}
		else
		{
			// Assume that given data contains 0 and 1s
			data[i] = '0';
		}
	}
	// Display result
	printf("\n 1's complement : %s \n", data);
}
int main()
{
	char data[] = "10010110";
	oneScomplement(data);
	return 0;
}

Output

 Given Binary   : 10010110
 1's complement : 01101001
// Java Program 
// Find 1s complement of a binary string

public class Complement
{
    public void oneScomplement(String data)
    {
        // Get the number of character in given data
        int n = data.length();
        String result = "";
        System.out.print(" Given Binary   : " + data );
        // Change binary string into 1's complement
        for (int i = 0; i < n; ++i)
        {
            if (data.charAt(i) == '0')
            {
                result += "1";
            }
            else
            {
                // Assume that given data contains 0 and 1s
                result += "0";
            }
        }
        // Display result
        System.out.print("\n 1's complement : " + result + " \n");
    }
    public static void main(String[] args)
    {
        Complement task = new Complement();
        // Test
        task.oneScomplement("10010110");
    }
}

Output

 Given Binary   : 10010110
 1's complement : 01101001
// Include header file
#include <iostream>
#include <string>

using namespace std;
// C++ Program 
// Find 1s complement of a binary string
class Complement
{
	public: void oneScomplement(string data)
	{
		// Get the number of character in given data
		int n = data.length();
		string result = "";
		cout << " Given Binary   : " << data;
		// Change binary string into 1's complement
		for (int i = 0; i < n; ++i)
		{
			if (data[i] == '0')
			{
				result += "1";
			}
			else
			{
				// Assume that given data contains 0 and 1s
				result += "0";
			}
		}
		// Display result
		cout << "\n 1's complement : " << result << " \n";
	}
};
int main()
{
	Complement *task = new Complement();
	// Test
	task->oneScomplement("10010110");
	return 0;
}

Output

 Given Binary   : 10010110
 1's complement : 01101001
// Include namespace system
using System;
// Csharp Program 
// Find 1s complement of a binary string
public class Complement
{
	public void oneScomplement(String data)
	{
		// Get the number of character in given data
		int n = data.Length;
		String result = "";
		Console.Write(" Given Binary   : " + data);
		// Change binary string into 1's complement
		for (int i = 0; i < n; ++i)
		{
			if (data[i] == '0')
			{
				result += "1";
			}
			else
			{
				// Assume that given data contains 0 and 1s
				result += "0";
			}
		}
		// Display result
		Console.Write("\n 1\'s complement : " + result + " \n");
	}
	public static void Main(String[] args)
	{
		Complement task = new Complement();
		// Test
		task.oneScomplement("10010110");
	}
}

Output

 Given Binary   : 10010110
 1's complement : 01101001
package main
import "fmt"
// Go Program 
// Find 1s complement of a binary string

func oneScomplement(data string) {
	// Get the number of character in given data
	var n int = len(data)
	var result string = ""
	fmt.Print(" Given Binary   : ", data)
	// Change binary string into 1's complement
	for i := 0 ; i < n ; i++ {
		if data[i] == '0' {
			result += "1"
		} else {
			// Assume that given data contains 0 and 1s
			result += "0"
		}
	}
	// Display result
	fmt.Print("\n 1's complement : ", result, " \n")
}
func main() {

	// Test
	oneScomplement("10010110")
}

Output

 Given Binary   : 10010110
 1's complement : 01101001
<?php
// Php Program 
// Find 1s complement of a binary string
class Complement
{
	public	function oneScomplement($data)
	{
		// Get the number of character in given data
		$n = strlen($data);
		$result = "";
		echo(" Given Binary   : ".$data);
		// Change binary string into 1's complement
		for ($i = 0; $i < $n; ++$i)
		{
			if ($data[$i] == '0')
			{
				$result .= "1";
			}
			else
			{
				// Assume that given data contains 0 and 1s
				$result .= "0";
			}
		}
		// Display result
		echo("\n 1's complement : ".$result." \n");
	}
}

function main()
{
	$task = new Complement();
	// Test
	$task->oneScomplement("10010110");
}
main();

Output

 Given Binary   : 10010110
 1's complement : 01101001
// Node JS Program 
// Find 1s complement of a binary string
class Complement
{
	oneScomplement(data)
	{
		// Get the number of character in given data
		var n = data.length;
		var result = "";
		process.stdout.write(" Given Binary   : " + data);
		// Change binary string into 1's complement
		for (var i = 0; i < n; ++i)
		{
			if (data.charAt(i) == '0')
			{
				result += "1";
			}
			else
			{
				// Assume that given data contains 0 and 1s
				result += "0";
			}
		}
		// Display result
		process.stdout.write("\n 1's complement : " + result + " \n");
	}
}

function main()
{
	var task = new Complement();
	// Test
	task.oneScomplement("10010110");
}
main();

Output

 Given Binary   : 10010110
 1's complement : 01101001
#  Python 3 Program 
#  Find 1s complement of a binary string
class Complement :
	def oneScomplement(self, data) :
		#  Get the number of character in given data
		n = len(data)
		result = ""
		print(" Given Binary   : ", data, end = "")
		i = 0
		#  Change binary string into 1's complement
		while (i < n) :
			if (data[i] == '0') :
				result += "1"
			else :
				#  Assume that given data contains 0 and 1s
				result += "0"
			
			i += 1
		
		#  Display result
		print("\n 1's complement : ", result ," ")
	

def main() :
	task = Complement()
	#  Test
	task.oneScomplement("10010110")

if __name__ == "__main__": main()

Output

 Given Binary   :  10010110
 1's complement :  01101001
#  Ruby Program 
#  Find 1s complement of a binary string
class Complement 
	def oneScomplement(data) 
		#  Get the number of character in given data
		n = data.length
		result = ""
		print(" Given Binary   : ", data)
		i = 0
		#  Change binary string into 1's complement
		while (i < n) 
			if (data[i] == '0') 
				result += "1"
			else
 
				#  Assume that given data contains 0 and 1s
				result += "0"
			end

			i += 1
		end

		#  Display result
		print("\n 1\'s complement : ", result ," \n")
	end

end

def main() 
	task = Complement.new()
	#  Test
	task.oneScomplement("10010110")
end

main()

Output

 Given Binary   : 10010110
 1's complement : 01101001 
// Scala Program 
// Find 1s complement of a binary string
class Complement()
{
	def oneScomplement(data: String): Unit = {
		// Get the number of character in given data
		var n: Int = data.length();
		var result: String = "";
		print(" Given Binary   : " + data);
		var i: Int = 0;
		// Change binary string into 1's complement
		while (i < n)
		{
			if (data.charAt(i) == '0')
			{
				result += "1";
			}
			else
			{
				// Assume that given data contains 0 and 1s
				result += "0";
			}
			i += 1;
		}
		// Display result
		print("\n 1\'s complement : " + result + " \n");
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Complement = new Complement();
		// Test
		task.oneScomplement("10010110");
	}
}

Output

 Given Binary   : 10010110
 1's complement : 01101001
// Swift 4 Program 
// Find 1s complement of a binary string
class Complement
{
	func oneScomplement(_ text: String)
	{
		// Get the number of character in given data
		let n: Int = text.count;
		var result: String = "";
		print(" Given Binary   : ", text, terminator: "");
        let data = Array(text);
		var i: Int = 0;
		// Change binary string into 1's complement
		while (i < n)
		{
			if (data[i] == "0")
			{
				result += "1";
			}
			else
			{
				// Assume that given data contains 0 and 1s
				result += "0";
			}
			i += 1;
		}
		// Display result
		print("\n 1's complement : ", result ," ");
	}
}
func main()
{
	let task: Complement = Complement();
	// Test
	task.oneScomplement("10010110");
}
main();

Output

 Given Binary   :  10010110
 1's complement :  01101001
// Kotlin Program 
// Find 1s complement of a binary string
class Complement
{
	fun oneScomplement(data: String): Unit
	{
		// Get the number of character in given data
		val n: Int = data.length;
		var result: String = "";
		print(" Given Binary   : " + data);
		var i: Int = 0;
		// Change binary string into 1's complement
		while (i < n)
		{
			if (data.get(i) == '0')
			{
				result += "1";
			}
			else
			{
				// Assume that given data contains 0 and 1s
				result += "0";
			}
			i += 1;
		}
		// Display result
		print("\n 1's complement : " + result + " \n");
	}
}
fun main(args: Array < String > ): Unit
{
	val task: Complement = Complement();
	// Test
	task.oneScomplement("10010110");
}

Output

 Given Binary   : 10010110
 1's complement : 01101001


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