mirror of
https://github.com/TheAlgorithms/C
synced 2024-11-22 05:21:49 +03:00
feat: add hamming distance (#1200)
* 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>
This commit is contained in:
parent
5ef38b30f6
commit
e8d3811f12
@ -188,6 +188,7 @@
|
||||
* [12](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/12.c)
|
||||
* [1207](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/1207.c)
|
||||
* [121](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/121.c)
|
||||
* [124](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/124.c)
|
||||
* [125](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/125.c)
|
||||
* [1283](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/1283.c)
|
||||
* [13](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/13.c)
|
||||
@ -263,6 +264,7 @@
|
||||
* [461](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/461.c)
|
||||
* [476](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/476.c)
|
||||
* [485](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/485.c)
|
||||
* [50](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/50.c)
|
||||
* [509](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/509.c)
|
||||
* [520](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/520.c)
|
||||
* [53](https://github.com/TheAlgorithms/C/blob/HEAD/leetcode/src/53.c)
|
||||
@ -335,6 +337,7 @@
|
||||
|
||||
## Misc
|
||||
* [Demonetization](https://github.com/TheAlgorithms/C/blob/HEAD/misc/demonetization.c)
|
||||
* [Hamming Distance](https://github.com/TheAlgorithms/C/blob/HEAD/misc/hamming_distance.c)
|
||||
* [Lexicographic Permutations](https://github.com/TheAlgorithms/C/blob/HEAD/misc/lexicographic_permutations.c)
|
||||
* [Longest Subsequence](https://github.com/TheAlgorithms/C/blob/HEAD/misc/longest_subsequence.c)
|
||||
* [Mirror](https://github.com/TheAlgorithms/C/blob/HEAD/misc/mirror.c)
|
||||
|
62
misc/hamming_distance.c
Normal file
62
misc/hamming_distance.c
Normal file
@ -0,0 +1,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;
|
||||
}
|
Loading…
Reference in New Issue
Block a user