如何让 strcasecmp 比较字符串而不区分大小写
How can I get strcasecmp to compare strings without being case sensitive
我正在学习 C 语言的 cs50 在线课程,我有一个项目是编写一个函数,将字典(单词数据库)加载到内存中,有效地将其散列成散列 table 然后比较给定文本中的单词以检查拼写,必须比较不区分大小写。
完成此概述后,我遇到的主要问题是降低了大小写敏感性,因此我不确定下面的 strcasecmp 函数是怎么出错的。我知道内存泄漏,但稍后会解决它们。
下面是C:
中的代码
//for the universal hash function
#define BASE 256
// Represents a node in a hash table
typedef struct node
{
char word[LENGTH + 1];
struct node *next;
}
node;
// Number of buckets in hash table
const unsigned int N = 676;
// Hash table
node *table[N];
int word_count = 0;
// Returns true if word is in dictionary else false
//Require a search funtion
bool check(const char *word)
{
int hashIndex = hash(word);
for (node *tmp = table[hashIndex]; tmp != NULL; tmp = tmp->next)
{
if (strcasecmp(word, tmp->word) == 0)
{
return true;
}
}
return false;
}
// Hashes word to a number
// the dividing hash function is one I cited from the yale.edu page http://www.cs.yale.edu/homes/aspnes/pinewiki/C(2f)HashTables.html having worked with.
unsigned int hash(const char *word)
{
unsigned long m = 11;
unsigned long h;
unsigned const char *us;
//ensure element value is >= 0
us = (unsigned const char *) word;
h = 0;
while(*us != '[=10=]')
{
h = (h * BASE + *us) % m;
us++;
}
return (h % N);
}
当使用 CS50 提供的 check50 函数进行检查时,我得到:
> :( spell-checking is case-insensitive expected "MISSPELLED WOR...",
> not "MISSPELLED WOR..." Log running ./speller case/dict case/text...
> checking for output "MISSPELLED WORDS\n\n\nWORDS MISSPELLED: 0\nWORDS
> IN DICTIONARY: 1\nWORDS IN TEXT: 8\n"...
>
> Expected Output: MISSPELLED WORDS
>
>
> WORDS MISSPELLED: 0 WORDS IN DICTIONARY: 1 WORDS IN TEXT:
> 8 Actual Output: MISSPELLED WORDS
>
> foO fOo Foo fOO FoO FOo FOO
>
> WORDS MISSPELLED: 7 WORDS IN DICTIONARY: 1 WORDS IN TEXT:
> 8
>
> :( handles most basic words properly expected "MISSPELLED WOR...", not
> "MISSPELLED WOR..." Log running ./speller basic/dict basic/text...
> checking for output "MISSPELLED WORDS\n\n\nWORDS MISSPELLED: 0\nWORDS
> IN DICTIONARY: 8\nWORDS IN TEXT: 9\n"...
>
> Expected Output: MISSPELLED WORDS
>
>
> WORDS MISSPELLED: 0 WORDS IN DICTIONARY: 8 WORDS IN TEXT:
> 9 Actual Output: MISSPELLED WORDS
>
> The
>
> WORDS MISSPELLED: 1 WORDS IN DICTIONARY: 8 WORDS IN TEXT:
> 9
主要问题是:如何在不区分大小写的情况下比较两个字符串?
if (strcasecmp(word, tmp->word) == 0)
并不是单词大小写重要的唯一地方。 hash
呢?它还需要考虑字母大小写,例如 'A' 和 'a' 将散列为不同的值。
我正在学习 C 语言的 cs50 在线课程,我有一个项目是编写一个函数,将字典(单词数据库)加载到内存中,有效地将其散列成散列 table 然后比较给定文本中的单词以检查拼写,必须比较不区分大小写。
完成此概述后,我遇到的主要问题是降低了大小写敏感性,因此我不确定下面的 strcasecmp 函数是怎么出错的。我知道内存泄漏,但稍后会解决它们。
下面是C:
中的代码
//for the universal hash function
#define BASE 256
// Represents a node in a hash table
typedef struct node
{
char word[LENGTH + 1];
struct node *next;
}
node;
// Number of buckets in hash table
const unsigned int N = 676;
// Hash table
node *table[N];
int word_count = 0;
// Returns true if word is in dictionary else false
//Require a search funtion
bool check(const char *word)
{
int hashIndex = hash(word);
for (node *tmp = table[hashIndex]; tmp != NULL; tmp = tmp->next)
{
if (strcasecmp(word, tmp->word) == 0)
{
return true;
}
}
return false;
}
// Hashes word to a number
// the dividing hash function is one I cited from the yale.edu page http://www.cs.yale.edu/homes/aspnes/pinewiki/C(2f)HashTables.html having worked with.
unsigned int hash(const char *word)
{
unsigned long m = 11;
unsigned long h;
unsigned const char *us;
//ensure element value is >= 0
us = (unsigned const char *) word;
h = 0;
while(*us != '[=10=]')
{
h = (h * BASE + *us) % m;
us++;
}
return (h % N);
}
当使用 CS50 提供的 check50 函数进行检查时,我得到:
> :( spell-checking is case-insensitive expected "MISSPELLED WOR...",
> not "MISSPELLED WOR..." Log running ./speller case/dict case/text...
> checking for output "MISSPELLED WORDS\n\n\nWORDS MISSPELLED: 0\nWORDS
> IN DICTIONARY: 1\nWORDS IN TEXT: 8\n"...
>
> Expected Output: MISSPELLED WORDS
>
>
> WORDS MISSPELLED: 0 WORDS IN DICTIONARY: 1 WORDS IN TEXT:
> 8 Actual Output: MISSPELLED WORDS
>
> foO fOo Foo fOO FoO FOo FOO
>
> WORDS MISSPELLED: 7 WORDS IN DICTIONARY: 1 WORDS IN TEXT:
> 8
>
> :( handles most basic words properly expected "MISSPELLED WOR...", not
> "MISSPELLED WOR..." Log running ./speller basic/dict basic/text...
> checking for output "MISSPELLED WORDS\n\n\nWORDS MISSPELLED: 0\nWORDS
> IN DICTIONARY: 8\nWORDS IN TEXT: 9\n"...
>
> Expected Output: MISSPELLED WORDS
>
>
> WORDS MISSPELLED: 0 WORDS IN DICTIONARY: 8 WORDS IN TEXT:
> 9 Actual Output: MISSPELLED WORDS
>
> The
>
> WORDS MISSPELLED: 1 WORDS IN DICTIONARY: 8 WORDS IN TEXT:
> 9
主要问题是:如何在不区分大小写的情况下比较两个字符串?
if (strcasecmp(word, tmp->word) == 0)
并不是单词大小写重要的唯一地方。 hash
呢?它还需要考虑字母大小写,例如 'A' 和 'a' 将散列为不同的值。