在我的数据库中,我有很多很难记住的药品名称。我想给用户一种可能用错字搜索一些药物的信息。在这种情况下,与strcasecmp
进行比较不太好,因为它会在第一次不匹配后停止,因此我尝试计算整个字符串的差异,但是错字和某些垃圾之间的差异太小而无法区分。我可以使用任何有效的算法吗?
这是我的第一次尝试:
char d[] = "1,3-Indanedione";
char s1[] = "1,3-Indaendione";
char s2[] = "1,3-QQQ";
cout<<strcasecmp(d,s1)<<" "<<strcasecmp(d,s2)<<"\n";
输出:9 -8
第二:
char d[] = "1,3-Indanedione";
char s1[] = "1,3-Indaendione";
char s2[] = "1,3-QQQ";
int l = min(strlen(d), strlen(s1));
int diff = 0;
for (int i = 0; i < l; ++i)
{
int x = abs(tolower(d[i]) - tolower(s1[i]));
diff += x;
}
cout<<diff<<" ";
l = min(strlen(d), strlen(s2));
diff = 0;
for (int i = 0; i < l; ++i)
{
int x = abs(tolower(d[i]) - tolower(s2[i]));
diff += x;
}
cout<<diff<<"\n";
输出:18 24