Algorithms_in_C 1.0.0
Set of algorithms implemented in C.
Loading...
Searching...
No Matches
hamming_distance.c File Reference

Hamming distance algorithm implementation. More...

#include <assert.h>
#include <stdio.h>
Include dependency graph for hamming_distance.c:

Functions

int hamming_distance (char *str1, char *str2)
 for assert
 
static void test ()
 Self-test implementations.
 
int main ()
 Main function.
 

Detailed Description

Hamming distance algorithm implementation.

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

Function Documentation

◆ hamming_distance()

int hamming_distance ( char *  str1,
char *  str2 
)

for assert

for IO operations

Function to calculate the Hamming distance between two strings

Parameters
param1string 1
param2string 2
Returns
Hamming distance
22{
23 int i = 0, distance = 0;
24
25 while (str1[i] != '\0')
26 {
27 if (str1[i] != str2[i])
28 {
29 distance++;
30 }
31 i++;
32 }
33
34 return distance;
35}

◆ main()

int main ( void  )

Main function.

Returns
0 on exit
59{
60 test(); // run self-test implementations
61 return 0;
62}
static void test()
Self-test implementations.
Definition hamming_distance.c:41
Here is the call graph for this function:

◆ test()

static void test ( void  )
static

Self-test implementations.

Returns
void
42{
43 char str1[] = "karolin";
44 char str2[] = "kathrin";
45
46 assert(hamming_distance(str1, str2) == 3);
47
48 char str3[] = "00000";
49 char str4[] = "11111";
50
51 assert(hamming_distance(str3, str4) == 5);
52 printf("All tests have successfully passed!\n");
53}
int hamming_distance(char *str1, char *str2)
for assert
Definition hamming_distance.c:21
Here is the call graph for this function: