Check for Binary String Last Updated : 23 Nov, 2024 Comments Improve Suggest changes Like Article Like Report Try it on GfG Practice Given a string s, the task is to check if it is a binary string or not. A binary string is a string which only contains the characters '0' and '1'.Examples:Input: s = "01010101010"Output: trueInput: s = "geeks101"Output: false Approach:The idea is to iterate over all the characters of the string and if we encounter a character other than '0' or '1', then the input string is not a binary string. Otherwise, if all the characters are either '0' or '1', then the input string is a binary string. C++ // C++ Program to check if a string is // binary string or not #include <iostream> using namespace std; bool isBinary(string &s) { for (int i = 0; i < s.length(); i++) { // Check if the character is neither // '0' nor '1' if (s[i] != '0' && s[i] != '1') { return false; } } return true; } int main() { string s = "01010101010"; cout << (isBinary(s) ? "true" : "false"); return 0; } C // C Program to check if a string is // binary string or not #include <stdio.h> #include <string.h> #include <stdbool.h> bool isBinary(char *s) { for(int i = 0; i < strlen(s); i++) { // Check if the character is neither // '0' nor '1' if(s[i] != '0' && s[i] != '1') { return false; } } return true; } int main() { char s[] = "01010101010"; printf(isBinary(s) ? "true" : "false"); return 0; } Java // Java Program to check if a string is // binary string or not class GfG { static boolean isBinary(String s) { for (int i = 0; i < s.length(); i++) { // Check if the character is neither // '0' nor '1' if (s.charAt(i) != '0' && s.charAt(i) != '1') { return false; } } return true; } public static void main(String[] args) { String s = "01010101010"; System.out.println(isBinary(s)); } } Python # Python Program to check if a string is # binary string or not def isBinary(s): for i in range(len(s)): # Check if the character is neither # '0' nor '1' if s[i] != '0' and s[i] != '1': return False return True s = "01010101010" print("true" if isBinary(s) else "false") C# // C# Program to check if a string is // binary string or not using System; class GfG { static bool isBinary(string s) { for (int i = 0; i < s.Length; i++) { // Check if the character is neither // '0' nor '1' if (s[i] != '0' && s[i] != '1') { return false; } } return true; } static void Main() { string s = "01010101010"; Console.WriteLine(isBinary(s) ? "true" : "false"); } } JavaScript // JavaScript Program to check if a string is // binary string or not function isBinary(s) { for (let i = 0; i < s.length; i++) { // Check if the character is neither // '0' nor '1' if (s[i] !== '0' && s[i] !== '1') { return false; } } return true; } let s = "01010101010"; console.log(isBinary(s)); OutputTrueTime Complexity: O(n), where n is the length of input string s.Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Check for Binary String M mrityuanjay8vae Follow Improve Article Tags : Strings DSA strings Practice Tags : StringsStrings Similar Reads What is Binary String? A binary string is a string that only has two characters, usually the numbers 0 and 1, and it represents a series of binary digits. Binary String Variables:In computer programming, binary string variables are used to store binary data, which is data that is represented in a binary (base-2) format, r 9 min read Check if two strings are same or not Given two strings, the task is to check if these two strings are identical(same) or not. Consider case sensitivity.Examples:Input: s1 = "abc", s2 = "abc" Output: Yes Input: s1 = "", s2 = "" Output: Yes Input: s1 = "GeeksforGeeks", s2 = "Geeks" Output: No Approach - By Using (==) in C++/Python/C#, eq 7 min read Convert String into Binary Sequence Given a string of character the task is to convert each character of a string into the equivalent binary number. Examples : Input : GFG Output : 1000111 1000110 1000111 Input : geeks Output : 1100111 1100101 1100101 1101011 1110011 The idea is to first calculate the length of the string as n and the 5 min read Bitonic string Given a string str, the task is to check if that string is a Bitonic String or not. If string str is Bitonic String then print "YES" else print "NO". A Bitonic String is a string in which the characters are arranged in increasing order followed by decreasing order of their ASCII values. Examples: In 6 min read Check if an encoding represents a unique binary string Given an encoding of a binary string of length k, the task is to find if the given encoding uniquely identifies a binary string or not. The encoding has counts of contiguous 1s (separated by 0s). For example, encoding of 11111 is {5}, encoding of 01101010 is {2, 1, 1} and encoding of 111011 is {3, 2 6 min read Like