【问题标题】:How can I get strcasecmp to compare strings without being case sensitive如何让 strcasecmp 比较字符串而不区分大小写
【发布时间】:2020-05-04 11:20:09
【问题描述】:

我正在学习 C 语言的 cs50 在线课程,我有一个项目是编写一个函数,将字典(单词数据库)加载到内存中,有效地将其散列到散列表中,然后进行比较给定文本中的单词要检查拼写,必须在不区分大小写的情况下进行比较。

我通过这个概述完成了我遇到的主要问题是降低了我的大小写敏感性,因此我不确定我在下面的 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 != '\0') 
        {
            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

主要问题是:如何在不区分大小写的情况下比较两个字符串?

【问题讨论】:

  • unload 函数只释放每个列表中的第一个条目。

标签: c cs50


【解决方案1】:

这个if (strcasecmp(word, tmp->word) == 0) 并不是唯一一个区分大小写的地方。 hash 呢?它还需要考虑字母大小写,例如,“A”和“a”将散列为不同的值。

【讨论】:

  • 谢谢我没有考虑到这一点,但是使用 tolower() 之类的东西将任何进入散列函数的内容更改为标准情况会更简单,从而消除问题还是一个坏主意?
  • 另一个问题我已经解决了除一个之外的所有问题。随着新的检查实施。字符低 [LENGTH + 1]; for (int i = 0, n = strlen(word); i
  • 这到底是什么意思?
  • low 未正确以 null 终止
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-27
  • 1970-01-01
相关资源
最近更新 更多