-3

I have a c program that takes in string inputs i want to check if these inputs are binary values how do I go about this?

For example if the user inputs "1001" how can I can I check to see if its binary or not try not to use pointers Please do it in c as well Also do not use the math library

@Mywork so far I converted the string to an integer and used the atoi function Here is the function I am checking the integer with

@ Program Re-explained I am using a scanf function to take in two inputs and I store them as a string THe user is supposed to enter two binary numbers. I then want to check and make sure each on of these numbers are binary. Below are some examples

INput: 101001 100101
Both Stored As String.
Output:function checks and see thats they are both binary

     That is a correct way I want it to run. Here is another example
   Input:1001hshds101 100101
   Both stored as String
   Output: Checks both strings and knows that the first string is wrong







//SOme of my work (ignore this for the most part)
int binCheck(long long int input){
      int dv;
       while(input! = 0){
        dv = input%10;
       if(dv>1){
           return 0;
          }
       input = input/10;
    }
    return 1;
    }
dbush
  • 205,898
  • 23
  • 218
  • 273
  • 4
    this sound like another homework. what have you tried so far? show us some code and we will help – Seek Addo Jan 20 '17 at 15:38
  • @SeekAddo just added some code. – donaldTheProgrammer Jan 20 '17 at 15:46
  • I suppose the definition of "binary" here is a number whose decimal representation contains only 0 and 1, correct me if I'm wrong. Your `binCheck` function seems correct to me. So where is the actual problem ? – Jabberwocky Jan 20 '17 at 15:47
  • @MichaelWalz yes thats correct, the problem is if the user enters "101hsjfh" it doesnt work. – donaldTheProgrammer Jan 20 '17 at 15:57
  • Your code does not do the string handling bit – Ed Heal Jan 20 '17 at 16:00
  • 1
    @donaldTheProgrammer Please show us the relevant code. But anyway from what I have understood from your description this is quite normal, `atoi` stops at the first non numeric digit, so if your string is `"101hsf"`, `atoi` will simple return 101, ignoring `"hsf"`. Why don't you just scanf the string and check directly for characters other that '1' and '0' ? This code would be shorter, no for `binCheck` neither `atoi` and it would do what you want. – Jabberwocky Jan 20 '17 at 16:05
  • See [Correct usage of `strtol()`](http://stackoverflow.com/questions/14176123) for how to use `strtol()`, but amongst other things, it will allow you to detect the `h` in `101hsjfh` and you can reject the input as invalid then. – Jonathan Leffler Jan 20 '17 at 16:49
  • @donaldTheProgrammer please update your question and tell us the __exact__ requirements of your program and show us also several examples of input (valid and invalid) and desired output. – Jabberwocky Jan 20 '17 at 16:52
  • I updated my code about what Im looking for – donaldTheProgrammer Jan 20 '17 at 17:32

1 Answers1

1

Don't use atoi(). Just loop through each character of the string and make sure that each character is either a '0' or '1'. Example below.

#include <stdio.h>
#include <string.h>

int isBinary( char *value );


int isBinary( char *value )
{
    int i;
    int bIsBinary = 0;
    int len;

    bIsBinary = 1;

    len = strlen( value );
    for( i = 0; i != len; i++ )
    {
        if ( value[i] != '0' && value[i] != '1' )
        {
            bIsBinary = 0;
            break;
        }
    }
    return( bIsBinary );
}


int main (void)
{
    char *value = "101a001";
    int isBin;

    isBin = isBinary( value );
    printf( "[%s] is Binary::%s\n", value, (isBin) ? "true":"false" );

    return( 0 );
}
Chimera
  • 5,884
  • 7
  • 49
  • 81