Check if string contains only 0 and 1

Here given code implementation process.

// C Program for
// Check if string contains only 0 and 1
#include <stdio.h>
#include <string.h>

void isBinaryText(char *text)
{
	int n = strlen(text);
	printf("\n Given text  : %s ", text);
	int i = 0;
	while (i < n)
	{
		if (!(text[i] == '0' || text[i] == '1'))
		{
			printf("\n  No ");
			return;
		}
		i++;
	}
	printf("\n  Yes ");
}
int main(int argc, char const *argv[])
{
	// Test
	isBinaryText("0200011");
	isBinaryText("010100111");
	return 0;
}

Output

 Given text  : 0200011
  No
 Given text  : 010100111
  Yes
// Java program for
// Check if string contains only 0 and 1
public class BinaryText
{
	public void isBinaryText(String text)
	{
		int n = text.length();
		System.out.print("\n Given text : " + text);
		int i = 0;
		while (i < n)
		{
			if (!(text.charAt(i) == '0' || text.charAt(i) == '1'))
			{
				System.out.print("\n No ");
				return;
			}
			i++;
		}
		System.out.print("\n Yes ");
	}
	public static void main(String[] args)
	{
		BinaryText task = new BinaryText();
		// Test
		task.isBinaryText("0200011");
		task.isBinaryText("010100111");
	}
}

Output

 Given text : 0200011
 No
 Given text : 010100111
 Yes
// Include header file
#include <iostream>
#include <string>

using namespace std;
// C++ program for
// Check if string contains only 0 and 1
class BinaryText
{
	public: void isBinaryText(string text)
	{
		int n = text.length();
		cout << "\n Given text : " << text;
		int i = 0;
		while (i < n)
		{
			if (!(text[i] == '0' || text[i] == '1'))
			{
				cout << "\n No ";
				return;
			}
			i++;
		}
		cout << "\n Yes ";
	}
};
int main()
{
	BinaryText *task = new BinaryText();
	// Test
	task->isBinaryText("0200011");
	task->isBinaryText("010100111");
	return 0;
}

Output

 Given text : 0200011
 No
 Given text : 010100111
 Yes
package main
import "fmt"
// Go program for
// Check if string contains only 0 and 1
type BinaryText struct {}
func getBinaryText() * BinaryText {
	var me *BinaryText = &BinaryText {}
	return me
}
func(this BinaryText) isBinaryText(text string) {
	var n int = len(text)
	fmt.Print("\n Given text : ", text)
	var i int = 0
	for (i < n) {
		if !(text[i] == '0' || text[i] == '1') {
			fmt.Print("\n No ")
			return
		}
		i++
	}
	fmt.Print("\n Yes ")
}
func main() {
	var task * BinaryText = getBinaryText()
	// Test
	task.isBinaryText("0200011")
	task.isBinaryText("010100111")
}

Output

 Given text : 0200011
 No
 Given text : 010100111
 Yes
// Include namespace system
using System;
// Csharp program for
// Check if string contains only 0 and 1
public class BinaryText
{
	public void isBinaryText(String text)
	{
		int n = text.Length;
		Console.Write("\n Given text : " + text);
		int i = 0;
		while (i < n)
		{
			if (!(text[i] == '0' || text[i] == '1'))
			{
				Console.Write("\n No ");
				return;
			}
			i++;
		}
		Console.Write("\n Yes ");
	}
	public static void Main(String[] args)
	{
		BinaryText task = new BinaryText();
		// Test
		task.isBinaryText("0200011");
		task.isBinaryText("010100111");
	}
}

Output

 Given text : 0200011
 No
 Given text : 010100111
 Yes
<?php
// Php program for
// Check if string contains only 0 and 1
class BinaryText
{
	public	function isBinaryText($text)
	{
		$n = strlen($text);
		echo("\n Given text : ".$text);
		$i = 0;
		while ($i < $n)
		{
			if (!($text[$i] == '0' || $text[$i] == '1'))
			{
				echo("\n No ");
				return;
			}
			$i++;
		}
		echo("\n Yes ");
	}
}

function main()
{
	$task = new BinaryText();
	// Test
	$task->isBinaryText("0200011");
	$task->isBinaryText("010100111");
}
main();

Output

 Given text : 0200011
 No
 Given text : 010100111
 Yes
// Node JS program for
// Check if string contains only 0 and 1
class BinaryText
{
	isBinaryText(text)
	{
		var n = text.length;
		process.stdout.write("\n Given text : " + text);
		var i = 0;
		while (i < n)
		{
			if (!(text.charAt(i) == '0' || text.charAt(i) == '1'))
			{
				process.stdout.write("\n No ");
				return;
			}
			i++;
		}
		process.stdout.write("\n Yes ");
	}
}

function main()
{
	var task = new BinaryText();
	// Test
	task.isBinaryText("0200011");
	task.isBinaryText("010100111");
}
main();

Output

 Given text : 0200011
 No
 Given text : 010100111
 Yes
#  Python 3 program for
#  Check if string contains only 0 and 1
class BinaryText :
	def isBinaryText(self, text) :
		n = len(text)
		print("\n Given text : ", text, end = "")
		i = 0
		while (i < n) :
			if (not(text[i] == '0'
					or text[i] == '1')) :
				print("\n No ", end = "")
				return
			
			i += 1
		
		print("\n Yes ", end = "")
	

def main() :
	task = BinaryText()
	#  Test
	task.isBinaryText("0200011")
	task.isBinaryText("010100111")

if __name__ == "__main__": main()

Output

 Given text :  0200011
 No
 Given text :  010100111
 Yes
#  Ruby program for
#  Check if string contains only 0 and 1
class BinaryText 
	def isBinaryText(text) 
		n = text.length
		print("\n Given text : ", text)
		i = 0
		while (i < n) 
			if (!(text[i] == '0' || text[i] == '1')) 
				print("\n No ")
				return
			end

			i += 1
		end

		print("\n Yes ")
	end

end

def main() 
	task = BinaryText.new()
	#  Test
	task.isBinaryText("0200011")
	task.isBinaryText("010100111")
end

main()

Output

 Given text : 0200011
 No 
 Given text : 010100111
 Yes 
import scala.collection.mutable._;
// Scala program for
// Check if string contains only 0 and 1
class BinaryText()
{
	def isBinaryText(text: String): Unit = {
		var n: Int = text.length();
		print("\n Given text : " + text);
		var i: Int = 0;
		while (i < n)
		{
			if (!(text.charAt(i) == '0' || text.charAt(i) == '1'))
			{
				print("\n No ");
				return;
			}
			i += 1;
		}
		print("\n Yes ");
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: BinaryText = new BinaryText();
		// Test
		task.isBinaryText("0200011");
		task.isBinaryText("010100111");
	}
}

Output

 Given text : 0200011
 No
 Given text : 010100111
 Yes
import Foundation;
// Swift 4 program for
// Check if string contains only 0 and 1
class BinaryText
{
	func isBinaryText(_ data: String)
	{
      	let text = Array(data);
		let n: Int = text.count;
		print("\n Given text : ", data, terminator: "");
		var i: Int = 0;
		while (i < n)
		{
			if (!(text[i] == "0" || text[i] == "1"))
			{
				print("\n No ", terminator: "");
				return;
			}
			i += 1;
		}
		print("\n Yes ", terminator: "");
	}
}
func main()
{
	let task: BinaryText = BinaryText();
	// Test
	task.isBinaryText("0200011");
	task.isBinaryText("010100111");
}
main();

Output

 Given text :  0200011
 No
 Given text :  010100111
 Yes
// Kotlin program for
// Check if string contains only 0 and 1
class BinaryText
{
	fun isBinaryText(text: String): Unit
	{
		val n: Int = text.length;
		print("\n Given text : " + text);
		var i: Int = 0;
		while (i < n)
		{
			if (!(text.get(i) == '0' || text.get(i) == '1'))
			{
				print("\n No ");
				return;
			}
			i += 1;
		}
		print("\n Yes ");
	}
}
fun main(args: Array < String > ): Unit
{
	val task: BinaryText = BinaryText();
	// Test
	task.isBinaryText("0200011");
	task.isBinaryText("010100111");
}

Output

 Given text : 0200011
 No
 Given text : 010100111
 Yes


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