Check String for Binary

PROBLEM :

Given a non-empty sequence of characters, return true if sequence is Binary, else return false

Input:

The task is to complete the method that takes a string as argument. We should not read any input from stdin/console. There are multiple test cases. For each test case, this method will be called individually.

Output:
Your function should return true str is binary, else false

Constraints:
1<=T<=50
1<=Length of str<=10000

Example:

Input:
2
101
75

Output:
1
0
     
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------

// Return true if str is binary, else false

bool isBinary(string &str)
{
   int l ;
   l=str.size() ;
 
   int i ;
   for(i=0;i<l;i++)
    {  
        if((str[i]=='1')||(str[i]=='0'))
            continue ;
        else
            return false ;
    }
           
    return true ;
}

---------------------------------------------------------------------------------

Comments

Popular posts from this blog

Count ways to N'th Stair(Order does not matter)

Replace all ‘0’ with ‘5’ in an input Integer

Chocolate Distribution Problem

Remove characters from the first string which are present in the second string

Primality Test ( CodeChef Problem code: PRB01 )