Home > Software design >  Adding 2 binary strings
Adding 2 binary strings

Time:10-13

I'm passing almost all leetCode tests with this, but not understanding why the output is wrong ("/0") when the input is: a = "10100000100100110110010000010101111011011001101110111111111101000000101111001110001111100001101" b = "110101001011101110001111100110001010100001101011101010000011011011001011101111001100000011011110011" Anyone has an idea to what is not working ? Thanks

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

char * sumBinary(long int binary1, long int binary2, char * result);

char * addBinary(char * a, char * b)
{
    char * result;
    long int a_int;
    long int b_int;

    a_int = atoi(a);
    b_int = atoi(b);
    result = malloc(sizeof(*result) * 1000);
    if (!result)
        return (NULL);
    sumBinary(a_int, b_int, result);
    return (result);
}

char * sumBinary(long int binary1, long int binary2, char * result)
{
    int i;
    int t;
    int rem;
    int sum[1000];

    i = 0;
    t = 0;
    rem = 0;
    
     if ((binary1 == 0) && (binary2 == 0))
    {
        result[0] = '0';
        result[1] = '\0';
    }  
    else
    {
        while (binary1 != 0 || binary2 != 0)
        {
            sum[i  ] = (binary1    binary2 % 10   rem) % 2;
            rem = (binary1    binary2 % 10   rem) / 2;
            binary1 = binary1 / 10;
            binary2 = binary2 / 10;
        }
        if (rem != 0)
            sum[i  ] = rem;
        --i;
        while (i >= 0)
        {
            result[t] = sum[i]   '0'; 
            t  ;
            i--;
        }
        result[t] = '\0';
        }
    return (result);
}

CodePudding user response:

For a start, you should be using atol(3), not atoi(3) if you're using long int. But that's not the main issue here.

atol(3) and atoi(3) expect strings containing decimal numbers, not binary, so that's not going to work well for you. You would need strtol(3), which you can tell to expect a string in ASCII binary. But again, this is not the main issue.

You don't give the question text, but I'm guessing they want you to add two arbitrarily-long ASCII-binary strings, resulting in an ASCII-binary string.

I imagine their expectation, given it's arbitrarily-long, is that you would be working entirely in the string domain. So you'd allocate for a string whose length is two greater than the longer of the two you get as parameters ( 1 for the terminal NUL, the other 1 for a potential overflow digit).

Then you start from the end, working back to the start, adding the corresponding digits of the parameter strings, placing the results into the result string starting from its end (allowing for that terminal NUL), adding as if you were doing it by hand.

Don't forget to add a leading zero to the result string, if you don't overflow into that position.

Note that I'm not going to write the code for you. This is either a learning exercise or a test: either way, you need to do the coding so you can learn from it.

  • Related