Skip to content

Instantly share code, notes, and snippets.

@mandrewstuart
Created September 12, 2019 15:22
Show Gist options
  • Save mandrewstuart/fd505e8e83a5b1be16a3db0e4469c3bc to your computer and use it in GitHub Desktop.
Save mandrewstuart/fd505e8e83a5b1be16a3db0e4469c3bc to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <string.h>
void substring(const char [], char[], int, int);
float fuzzy_string(const char* a, const char* b) {
int reward = 0;
int penalty = 0;
int len_a = 0;
while (a[len_a] != '\0') len_a++;
int len_b = 0;
char sub[64];
while (b[len_b] != '\0') len_b++;
for (int start = 0; start < len_a; start++) {
for (int length = 0; length < len_a - 1; length++) {
substring(a, sub, start, length);
if(strstr(b, sub) != NULL) {
reward += length * 2;
} else {
penalty += 1;
}
}
}
for (int start = 0; start < len_b; start++) {
for (int length = 0; length < len_b - 1; length++) {
substring(b, sub, start, length);
if(strstr(a, sub) != NULL) {
reward += length * 2;
} else {
penalty += 1;
}
}
}
float total = reward + penalty;
return (float) reward / total;
}
void substring(const char s[], char sub[], int p, int l) {
int c = 0;
while (c < l) {
sub[c] = s[p+c-1];
c++;
}
sub[c] = '\0';
}
int main() {
float x = fuzzy_string("andrew", "mandrew");
printf("%f\n", x);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment