gpt4 book ai didi

比较两个字符串,字典程序

转载 作者:行者123 更新时间:2023-11-30 15:14:08 24 4
gpt4 key购买 nike

到目前为止,我正在用 C 编写一个字典程序,我的程序打开一个 txt 文件(字典),然后打开另一个 txt 文件以检查拼写,此时它会将文档中的每个单词与字典文件中的每个单词进行比较打印出文档中拼写错误的第一个单词,但是永远不会回到内部 while 循环,我不知道如何解决这个问题

这是我的代码的最后一部分,请帮忙!

//compare document to dictionary 

char dictword[300];
char misword[300];
FILE *fp1, *fp2;

fp2 = fopen("dictionary.txt","r"); //open dictionary
fp1 = fopen("mispelled.txt","r"); //open document to be checked for spelling


while (test != EOF)//run loop until every misword file has been checked
{
test = fscanf(fp1,"%s",misword); //gets one word from the dictionary file

while (test2 != EOF)//run loop until every word in dict file has been checked
{
test2 = fscanf(fp2,"%s",dictword); //gets one word from the dictionary file

if (0==strcmp(misword,dictword))//if misword is = to dictword
break; //if a word in the misword file matches the dictionary word it must be spelled correct, break out of loop

else if(test2 == EOF)//if one misword is compared with every dictword misword must not be a word
printf("%s This word is not found in the dictionary \n",misword); //thus print it
}

} //while (test != EOF);//run loop until every misword file has been checked
fclose(fp2);
fclose(fp1);

最佳答案

看起来你的内循环逻辑是错误的。您需要遍历字典中的每个单词来查看它是否拼写错误。一旦你在字典中找到了这个单词,你就可以像你一样跳出内循环。但是,在跳出内部循环之前,您应该设置一个标志来指示您找到了该单词。这样,在完成内部循环后,您可以检查标志以查看是否找到该单词。另请注意,此实现将非常缓慢。程序通常会将字典放入哈希表中,以使查找更加高效。

关于比较两个字符串,字典程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34169625/

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