gpt4 book ai didi

C程序: strcasecmp() not working

转载 作者:行者123 更新时间:2023-11-30 15:22:58 26 4
gpt4 key购买 nike

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

 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com