1

I have to find the hamming distance between two codes.

For example if I input:

a= 10

b= 1010

Automatically a should be made equal to the length of the string b by appending 0's.

So the input should become:

a=0010

b=1010

But I'm getting instead:

a = 001010

b = 1010

Here is my code:

#include<stdio.h>
#include<string.h>
void main()
{
    char a[20],b[20],len1,len2,i,diff,count=0,j;
    printf("Enter the first binary string\n");
    scanf("%s",a);
    printf("Enter the second binary string\n");
    scanf("%s",b);

    len1 = strlen(a);
    len2 = strlen(b);

    if(len1>len2)
    {
        diff = len1-len2;

        for(i=0;i<len1;i++)
        {
            b[i+diff]=b[i];
        }

        j=i+diff;
        b[j]='\0';

        for(i=0;i<diff;i++)
        {
            b[i]='0';
        }
    }
    else
    {
        diff = len2-len1;

        for(i=0;i<len2;i++)
        {
            a[i+diff]=a[i];
        }

        j=i+diff;
        a[j]='\0';

        for(i=0;i<diff;i++)
        {
            a[i]='0';
        }
    }

    printf("\nCodes are\n");
    printf("a=%s\n",a);
    printf("\nb=%s\n",b);

    for(i=0;a[i]!='\0';i++)
    {
        if(a[i]!=b[i])
        {
            count++;
        }
    }

    printf("hammung distance between two code word is %d\n",count);
}

Can anyone help me to fix this issue?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Nithin
  • 53
  • 6

3 Answers3

2

In your two for loop where you are moving the content of your old tab to the right to insert the zeros, you inverted the lengths.

First loop should be:

for(i=0;i<len2;i++)
{
    b[i+diff]=b[i];
}

And second:

for(i=0;i<len1;i++)
{
    a[i+diff]=a[i];
}

After trying it:

Codes are

a=0010

b=1010

hammung distance between two code word is 1

Also, the main function should return an int, not void. As stated in the comments, you should also change the type of your len1, len2, i, diff, count and j because you use them as number values, not as characters. You can for instance either use the int or size_t types for that.

int main()
{
    char a[20],b[20];
    int len1, len2, i, diff, count=0, j;
    // Rest of your code
}
Community
  • 1
  • 1
Izuka
  • 2,572
  • 5
  • 29
  • 34
1

Here is a method that does not prepend zeros to the shortest binary string, and avoids the limitations of strtol() by comparing the elements of the string directly, starting with the last characters. The intricacies of using strtol() are traded for more complexity in handling the array indices. Note that care must be taken to avoid counting down to a negative value since size_t types are used. This method is not limited by the capacity of long types, but rather by size_t.

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

int main(void)
{
    char a[20], b[20];
    printf("Enter first binary string: ");
    scanf("%19s", a);
    printf("Enter second binary string: ");
    scanf("%19s", b);

    size_t a_len = strlen(a);
    size_t b_len = strlen(b);
    size_t max_len = a_len > b_len ? a_len : b_len;

    size_t hamming_dist = 0;
    for (size_t i = 0; i < max_len; i++) {
        if (a_len - i > 0 && b_len - i > 0) {
            if (a[a_len - i - 1] == b[b_len - i - 1]) {
                continue;
            }
        }
        if ((a_len - i > 0 && a[a_len - i - 1] == '1') ||
            (b_len - i > 0 && b[b_len - i - 1] == '1')) {
            ++hamming_dist;
        }
    }

    printf("bstring_1: %s\n", a);
    printf("bstring_2: %s\n", b);
    printf("Hamming distance: %zu\n", hamming_dist);

    return 0;
}
ad absurdum
  • 19,498
  • 5
  • 37
  • 60
0

A way that doesn't need to pad one of the parameters with zeroes:

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

int main ()
{
    char *a = "1010";
    char *b = "10";

    long unsigned int xorab;
    unsigned int hammingDistance = 0;

    xorab = strtoul(a, NULL, 2) ^ strtoul(b, NULL, 2);

    while (xorab) {
        hammingDistance += xorab & 1;
        xorab >>= 1;
    }

    printf("%u\n", hammingDistance);   
}

It uses strtoul to convert the binary strings to unsigned long int using a base 2, then you only have to use bitwise operators (xor, and, shift) to calculate the Hamming distance without to take care of the size difference.

Obviously, this way stops to work if you want to test binary strings with values greater than an unsigned long int.

Casimir et Hippolyte
  • 88,009
  • 5
  • 94
  • 125