你怎么能不敏感地比较两个字符串标点符号?
how can you compare two strings punctuation insensitively?
我正忙着做一个拼写检查器,当我比较单词 I'm 和字典版本 im 时 returns 错误。我使用 strcasecmp
不区分大小写地比较它们,但撇号仍然是一个问题。如何比较两者(不区分标点符号)并得到 true 作为输出?
编写自己的跳过标点符号的例程:
#include <stdio.h>
#include <ctype.h>
int insenistive_strcmp(char const* s1, char const* s2)
{
unsigned char const* p1 = (unsigned char const*)s1;
unsigned char const* p2 = (unsigned char const*)s2;
for (;; ++p1, ++p2)
{
while (ispunct(*p1))
++p1;
while (ispunct(*p2))
++p2;
int ch1 = toupper(*p1);
int ch2 = toupper(*p2);
if (!ch1 || !ch2 || ch1 != ch2)
return ch1 - ch2;
}
}
int main()
{
printf("%d\n", insenistive_strcmp("I'm", "im"));
}
我正忙着做一个拼写检查器,当我比较单词 I'm 和字典版本 im 时 returns 错误。我使用 strcasecmp
不区分大小写地比较它们,但撇号仍然是一个问题。如何比较两者(不区分标点符号)并得到 true 作为输出?
编写自己的跳过标点符号的例程:
#include <stdio.h>
#include <ctype.h>
int insenistive_strcmp(char const* s1, char const* s2)
{
unsigned char const* p1 = (unsigned char const*)s1;
unsigned char const* p2 = (unsigned char const*)s2;
for (;; ++p1, ++p2)
{
while (ispunct(*p1))
++p1;
while (ispunct(*p2))
++p2;
int ch1 = toupper(*p1);
int ch2 = toupper(*p2);
if (!ch1 || !ch2 || ch1 != ch2)
return ch1 - ch2;
}
}
int main()
{
printf("%d\n", insenistive_strcmp("I'm", "im"));
}