I have a list of (alphabetical) strings, and I want to compare a string against a list of other strings, and find which string has the lowest Hamming distance compared to that string. Also, the list of strings used for comparison is known at compile time.
I thought of something like this:
char* compare[10][10]; // Assume this array is filled with random strings, every string of length n is in row n - 1.
int hamming_distance(char* x, char* y, int len); // I don't know of any fast ways to compute the Hamming distance between two alphabetical strings
int hamming_distance(char* x, char* y, int len) {
int hamming_distance = 0;
for(i = 0; i <= len; i++) {
if(x[i] != y[i]) {
hamming_distance++;
}
}
return hamming_distance;
}
char* lowest_hamming_distance(char* x);
char* lowest_hamming_distance(char* x) {
int minhammingdistance = INT_MAX;
int currenthammingdistance;
int len = strlen(x) - 1;
for( i = 0; i < 10; i++) {
currenthammingdistance = hamming_distance(compare[len][i], x, len);
if(minhammingdistance > currenthammingdistance) {
minhammingdistance = currenthammingdistance;
int min_hamming_distance_string = i;
}
}
return compare[len][min_hanmming_distance_string];
}