// Experiment 18: Calculate the HAMMING DISTANCE between two binary codewords
// Compile: gcc 18_hamming_distance.c -o hamming
// Run    : ./hamming

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

int main() {
    char a[100], b[100];

    printf("Enter first  binary codeword: ");
    if (scanf("%99s", a) != 1) return 1;
    printf("Enter second binary codeword: ");
    if (scanf("%99s", b) != 1) return 1;

    int la = strlen(a);
    int lb = strlen(b);

    if (la != lb) {
        printf("Error: codewords must be of equal length (%d vs %d)\n", la, lb);
        return 2;
    }

    // Also validate that each char is '0' or '1'
    for (int i = 0; i < la; i++) {
        if ((a[i] != '0' && a[i] != '1') ||
            (b[i] != '0' && b[i] != '1')) {
            printf("Error: codewords must contain only 0s and 1s.\n");
            return 3;
        }
    }

    int dist = 0;
    for (int i = 0; i < la; i++)
        if (a[i] != b[i]) dist++;

    printf("Hamming distance between '%s' and '%s' = %d\n", a, b, dist);
    return 0;
}
