-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
/
hamming_distance.c
62 lines (54 loc) · 1.28 KB
/
hamming_distance.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/**
* @file
* @brief [Hamming distance](https://en.wikipedia.org/wiki/Hamming_distance)
* algorithm implementation.
* @details
* In information theory, the Hamming distance between two strings of
* equal length is the number of positions at which the corresponding symbols
* are different.
* @author [Aybars Nazlica](https://github.com/aybarsnazlica)
*/
#include <assert.h> /// for assert
#include <stdio.h> /// for IO operations
/**
* @brief Function to calculate the Hamming distance between two strings
* @param param1 string 1
* @param param2 string 2
* @returns Hamming distance
*/
int hamming_distance(char* str1, char* str2)
{
int i = 0, distance = 0;
while (str1[i] != '\0')
{
if (str1[i] != str2[i])
{
distance++;
}
i++;
}
return distance;
}
/**
* @brief Self-test implementations
* @returns void
*/
static void test()
{
char str1[] = "karolin";
char str2[] = "kathrin";
assert(hamming_distance(str1, str2) == 3);
char str3[] = "00000";
char str4[] = "11111";
assert(hamming_distance(str3, str4) == 5);
printf("All tests have successfully passed!\n");
}
/**
* @brief Main function
* @returns 0 on exit
*/
int main()
{
test(); // run self-test implementations
return 0;
}