Skip to content

Hamming

We read DNA using the letters C,A,G and T. Two strands might look like this:

1
2
3
GAGCCTACTAACGGGAT
CATCGTAATGACGGCCT
^ ^ ^  ^ ^    ^^

They have 7 differences, and therefore the Hamming Distance is 7.

The Hamming Distance is useful for lots of things in science, not just biology, so it's a nice phrase to be familiar with :)

Task

Calculate the Hamming Distance between two DNA strands.

The Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
#include "hamming.h"
#include "stdio.h"
#include "string.h"
#include <stdlib.h>

int compute(const char *lhs, const char *rhs) {
if (*lhs == '\0' && *rhs == '\0')
    return 0;

if (strlen(lhs) != strlen(rhs))
    return -1;

int i = 0;
int ii = 0;
for (i = 0; i < (int)strlen(lhs); i++) {
    if (lhs[i] != rhs[i])
    ii++;
}
return ii;
}
1
2
3
4
5
6
#ifndef HAMMING_H
#define HAMMING_H

int compute(const char *lhs, const char *rhs);

#endif

Last update: February 8, 2021

Comments