mirror of
https://github.com/TheAlgorithms/C
synced 2024-11-22 13:31:21 +03:00
e8d3811f12
* feat: add hamming distance * Update misc/hamming_distance.c Co-authored-by: David Leal <halfpacho@gmail.com> * Update misc/hamming_distance.c Co-authored-by: David Leal <halfpacho@gmail.com> * updating DIRECTORY.md * Add curly braces to the while loop * Fix character comparison * Add a one-line description for library/header * Update misc/hamming_distance.c Co-authored-by: Taj <tjgurwara99@users.noreply.github.com> * Fix function names in test --------- Co-authored-by: David Leal <halfpacho@gmail.com> Co-authored-by: github-actions[bot] <github-actions@users.noreply.github.com> Co-authored-by: Taj <tjgurwara99@users.noreply.github.com>
63 lines
1.3 KiB
C
63 lines
1.3 KiB
C
/**
|
|
* @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;
|
|
}
|