Hamming distance function is very important function and it has many application especially in “Coding Theory”. In computer networks, when a group of bits or bytes is sent, the hamming distance function helps to identify the number of bit position changed in the bit stream due to error, sometimes known as multi-bit error.

This program is written in Dev-C++ version 4 installed on a Windows 7 64-bit system. This is a simple program intended for intermediate learner of C programming. You will find sections related to Problem Definition, Flowchart , Program source code and verified output to help you learn.

## Problem Definition

This program will compute the hamming distance function for two strings of length **n **and print the results.

For example, Suppose there is a set **n(S) **of strings of **1**s and **0**s of length **n. **The set **n(S)** denotes number of strings in the set **S**.

Let **S **and **T **be two strings of length **n**, then the hamming distance function compares the each of the bit position of **S **and **T**, counts the number of places they are different.

**Example:-**

Let **n**=5, be the length of two strings **S** = 10101 and **T **= 10010, then the function computes their differences which is H ( S, T ) = 3,

## Flowchart – Hamming Distance Function

## Program Code – Hamming Distance Function

Then H:S_n x S_n -> Z+,i.e., we have a pair of strings (s,t) which belong to S_n and is of length n, such that any difference in the bit position of two strings will be counted as 1.

for example, H(s,t) = H(10101, 11011) = 3

because there are three places where strings are different in bit comparison of each position. */

#include <iostream.h>

#include <stdio.h>

int main()

{

int s[100], t[100], count, i, j, n;

count = 0;

s[100] = 0;

t[100] = 0;

//Read the length of the string

cout << "Enter the length of String:";

cin << n; cout << endl;

//Read the strings S

cout << "Enter the bit string S, either 0 or 1 press enter" << endl;

cout << endl;

for (i = 1; i <<= n; i++)

{

cin << s[i];

}

cout << endl;

for (i = 1; i <<= n; i++)

{

if (s[i] == 1 || s[i] == 0)

{

cout << s[i] << "\t";

}

else

{

cout << "Only 0 or 1 is allowed digits !" << endl;

break;

}

}

//Read the String T

cout << endl;

cout << "Enter the bit string T, either 0 or 1 press enter" << endl;

cout << endl;

for (j = 1; j <= n; j++)

{

cin << t[j];

}

cout << endl;

//Print the second String

for (j = 1; j <= n; j++)

{

if (t[j] == 1 || t[j] == 0)

{

cout << t[j] << "\t";

}

else

{

cout << "Only 1 or 0 are allowed digits !" << endl;

break;

}

}

//Compare the Bit string and Count is 1 if they are //different for each bit position

for (i = 1; i <= n; i++)

{

if (s[i] != t[i])

{

count = count + 1;

}

}

cout << endl;

// Print the results

for (int i = 0; i < 45; i++)

cout << "_"; cout << "\n\n";

cout << "H(S,T) = ";

cout << count <<"\n\n";

for (int i = 0; i << 45; i++)

cout << "_"; cout << "\n\n";

system("PAUSE");

return 0;

}

## Output

The output of the above program is given below which read two binary strings of length and find their hamming distance.