Hass
Hass

Reputation: 25

How do I check if a string only contains one and zero?

I am trying to detect whether a string contains only the characters '0' and '1'. This is what I have so far:

    while (indexCheck < 32) {

        if ((input[indexCheck] != '0') && (input[indexCheck] != '1')) {

            printf("not binary ");
            indexCheck++;

        }  else if ((input[indexCheck] = '0') && (input[indexCheck] = '1')) {
            indexCheck++;
            printf("is binary ");

        } 

    }

I know why it returns "is binary" or "not binary" for every single character in the array, but I don't know how to fix this. I want it to return "is binary" once if the string is only made of '1' and '0', and the opposite if this is false. I'm new to C so all help is appreciated.

Upvotes: 1

Views: 6200

Answers (6)

Gor Asatryan
Gor Asatryan

Reputation: 924

    int isBinary = 1;
    while (input[indexCheck] != '\0')
    {
        if (input[indexCheck] != '1' && input[indexCheck] != '0')
        {
            isBinary = 0;
            break;
        }
        ++indexCheck;
    }
    if (isBinary)
    {
        printf("binary");
    }
    else
    {
        printf("not binary");
    }

Check each element in string input. If input[index] is not 0 or 1 the flag isBinary becomes 0 and breaks while. And you do not need length of string.

Upvotes: 1

anfauglit
anfauglit

Reputation: 161

You can stop looping through the string the moment you find a character which is neither '0' nor '1'. After the loop is terminated, you check whether or not you've reached the end of the string, i.e. the current character is a null character '\0'

while (*s == '0' || *s == '1') ++s;

if (*s)
    puts("not binary");
else
    puts("binary");

Upvotes: 2

vilidigonzales
vilidigonzales

Reputation: 29

there is a block of code for you with comments.

#include <stdio.h>
#include <stdlib.h>

#define STRING_SIZE 32 // Better to use #define for reusability

// Function prototype
int isBinary(char * testInput);

// Main program
int main(void)
{
    // Test inputs
    char testInputBinary[33] = "01010101010101010101010101010101";
    char testInputNotBinary[33] = "010101010101010101010101010101ab";

    // Test & collect results
    if (isBinary(testInputBinary))
    {
        printf("Binary ! \n");
    }
    else
    {
        printf("Not binary ! \n");
    }


    if (isBinary(testInputNotBinary))
    {
        printf("Binary ! \n");
    }
    else
    {
        printf("Not binary ! \n");
    }

    return EXIT_SUCCESS;
}

int isBinary(char * testInput)
{
    int loopIdx = 0; // Loop index
    int returnVal = 0; // 0: False, 1: True

    // iterate over string
    for (loopIdx = 0; loopIdx < STRING_SIZE; loopIdx++)
    {
        if(testInput[loopIdx] != '0' && testInput[loopIdx] != '1')
        {
            break;
        }
    }

    // If the loop is not broken, it means all characters are in binary form
    if (loopIdx == STRING_SIZE)
    {
        returnVal = 1;
    } // No need to writing else clause since returnVal = 0 at the beginning

    return returnVal;
}

Upvotes: 1

Paul Ogilvie
Paul Ogilvie

Reputation: 25286

You can do:

while (indexCheck < 32) 
{
    if ((input[indexCheck] != '0') && (input[indexCheck] != '1'))
    {
        break;
    }
    else
    {
        indexCheck++;
    } 
}
if (indexCheck == 32)
    printf("is binary ");
else
    printf("is not binary ");

Only when it has processed all elements and did not encounter a non 1-or-0 ends the loop with indexCheck == 32 so you can use that to determine what to print.

Note also that your else condition is not needed.

Upvotes: 1

Shawn
Shawn

Reputation: 52374

Instead of looping manually through the string, you can see if it only contains certain characters by checking to see if strspn() returns the length of the string (By seeing if the index of the value it returns is the 0 terminator at the end of the string):

_Bool is_binary(const char *s) {
  if (!s || !*s) {
    return 0;
  }
  return s[strspn(s, "01")] == '\0';
}

Upvotes: 13

mch
mch

Reputation: 9804

I would make a function for this:

int isBinary(const char *input)
{
    for (int i = 0; input[i]; ++i)
        if (input[i] != '0' && input[i] != '1')
            return 0;
    return 1;
}

Then you can call the function:

if (isBinary("0001110110101"))
    printf("is binary\n");
else
    printf("is not binary\n");

https://ideone.com/tKBCbf

Upvotes: 10

Related Questions