C程序: strcasecmp() not working

标签 c arrays struct

我正在尝试计算动态结构数组中单词出现的次数。但输出与所需的答案不匹配。对代码的任何指导或修改将不胜感激。

 void COUNT(struct wordsStruct *allWords, int size)
{
    int boolean = 0;
    int count = 0;

    for (int i = 0; i < size; i++)
    {
        count = 0;
        for (int j = i + 1; j < size; j++)
        {
            printf("Comparing %s with %s \n", (allWords[i].name, allWords[j].name));
            if (strcasecmp(allWords[i].name, allWords[j].name) == 0)
            {
                boolean = 1;
                count++;
            }
            else
            {
                boolean = 0;
            }
        }
        if (boolean == 1 && count != 0)
        {
            allWords[i].number = count;
        }
    }
}

int main(int argc, char** argv)
{
    // Declaring Variables
    FILE *fileReader;
    int numberOfWords;
    struct wordsStruct *container;

    if (argc == 1)
    {
        printf("More Arguments Needed. \n");
    }
    else if (argc == 2)
    {
        numberOfWords = SCAN(&fileReader, argv[1]);
        printf("Number Of Words: %d. \n", numberOfWords);
        container = LOAD(fileReader, numberOfWords, argv[1]);

        qsort(container, numberOfWords, sizeof(struct wordsStruct), compare);

        COUNT(container, numberOfWords);

        for (int i=0; i<numberOfWords; i++)
        {
            printf("WORD: %s. \t", container[i].name);
            printf("Occurence: %d. \n", container[i].number);
        }
    }       
    return (0);
}

    typedef struct wordsStruct
    {
        char* name;
        int number;
    }wordsStruct;

//INPUT FILE
My
Code
ZZZ
ZZZ
Is
Not
zzz
ZZZ
zzz
Working

//Output
WORD: Code.     Occurence: 1. 
WORD: Is.   Occurence: 1. 
WORD: My.   Occurence: 1. 
WORD: Not.  Occurence: 1. 
WORD: Working.  Occurence: 1. 
WORD: Working.  Occurence: 1. 
WORD: zzz.  Occurence: 4. 
WORD: ZZZ.  Occurence: 3.   // THIS SHOULD BE 5?
WORD: zzz.  Occurence: 2. 
WORD: ZZZ.  Occurence: 1. 
WORD: ZZZ.  Occurence: 1.

最佳答案

您错误地认为 strcasecmp 不起作用。

您的代码中有一些逻辑错误。

for (int j = i + 1; j < size; j++)
{
    printf("Comparing %s with %s \n", (allWords[i].name, allWords[j].name));
    if (strcasecmp(allWords[i].name, allWords[j].name) == 0)
    {
        boolean = 1;
        count++;
    }
    else
    {
        boolean = 0;
    }
}

上面的代码块存在以下问题:

  1. 发现不匹配的单词后它不会停止。
  2. 当列表中的最后一个单词与索引 i 处的单词不匹配时,会将 boolean 设置为 0,即用于检查是否找到匹配项。

试试这个:

for (int i = 0; i < size; i++)
{
   count = 0;
   int j;
   for ( j = i + 1; j < size; j++)
   {
      printf("Comparing %s with %s \n", (allWords[i].name, allWords[j].name));
      if (strcasecmp(allWords[i].name, allWords[j].name) == 0)
      {
         boolean = 1; // Don't need this at all.
         count++;
      }
      else
      {
         break;
      }
   }

   if (count != 0)
   {
      allWords[i].number = count;
   }

   // If there are three matching words, j would be i + 3;
   // We don't need to compare the words at i+1, i+2 again.
   // Change i to be the index of the next word for comparison.
   i = j-1;
}

更新

关于的部分

allWords[i].number = count;

需要做得更好。

for (int i = 0; i < size; i++)
{
   // The count is at least 1
   count = 1;
   int j;
   for ( j = i + 1; j < size; j++)
   {
      printf("Comparing %s with %s \n", (allWords[i].name, allWords[j].name));
      if (strcasecmp(allWords[i].name, allWords[j].name) == 0)
      {
         boolean = 1; // Don't need this at all.
         count++;
      }
      else
      {
         break;
      }
   }

   for ( ; i < j; ++i ) 
   {
      allWords[i].number = count;
   }
}

关于C程序: strcasecmp() not working,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28956647/

相关文章:

c - 使用结构体指针的两个函数(读取和显示数组)

arrays - 如何使用 ng-repeat 遍历 JSON 中的数组数组?

c - 如何使用指针访问结构中列表中的结构?

c++ - OpenProcessToken 从本地系统服务失败并出现 ERROR_ACCESS_DENIED

C:正则表达式优化 |保存状态

c - 在字符指针中搜索 '\n 使用 c

arrays - 如何将数组转换为元组?

JavaScript 不区分大小写的数组通用排序

c - malloc 指针上的偶发段错误

python - 如何在Python中将16位十六进制和二进制表示形式转换为十进制 float (反之亦然)?