比较两个文本文件 - C 中的拼写检查程序 [英] Compare two text files - spellchecking program in C

查看:12
本文介绍了比较两个文本文件 - C 中的拼写检查程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个拼写检查程序,该程序将用户的文本文件与字典进行比较,以查看他们输入的单词是否在字典中.如果不是,则会打印一条错误消息,告诉用户该特定单词是错误的.我已经尝试了下面代码的多种变体,但没有得到想要的结果.它是嵌套的 while 循环中的某些东西将其抛出.此代码处于草稿阶段,我必须使其更有效地使用内存等并进行整理.我只是想先让它工作.谢谢!

I'm writing a spellchecking program that will compare a user's text file with a dictionary to see if the words they entered are in the dictionary. If not, an error message is printed to tell the user that the specific word is wrong. I've tried a number of variations of the code below but not getting the desired results. It's something in the nested while loop that's throwing it out. This code is in draft stage I have to make it more memory efficient etc and tidy it up. I'm just interested in getting it working first. Thanks!

按照下面的提示稍微修改了代码.它现在读取第一个单词并说它在字典中.然后它显示第二个单词,但字典扫描循环没有运行并且程序挂起.我知道它的嵌套 while 循环导致了我无法理解的问题!

Have altered the code slightly as per the tips below. It now reads the first word and says that it is in the dictionary. It then displays the second word but the dictionary scanning loop doesn't run and the program hangs. I know its the nested while loop causing the issue I just can't get my head around it!

/*Spellcheck program*/
/*Author: */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(void)
{
/*Open files and test that they open*/
FILE *fp1;
FILE *fp2;
char fname[20];
char wordcheck[45];/*The longest word in the English Language is 45 letters long*/
char worddict[45];
char dummy;
int i;
int dictcount = 0;

fp1 = fopen("dictionary.txt","r");

if (fp1 == NULL)
{
printf("The dictionary file did not open.");
exit(0);
}

printf("Please enter the path of the file you wish to check:
");
scanf("%s", fname);
scanf("%c", &dummy);

fp2 = fopen(fname, "r");
    if (fp2 == NULL)
        {
        printf("Your file did not open, please check your filepath and try again.
");

        printf("Please enter path of file you wish to check: 
");
        scanf("%20s",fname);

        fp2 = fopen(fname, "r");
        }

    else
        {
        printf("Your file opened correctly
");
        }

/*When files are open, read each word from the text file into an array:*/

    while(fscanf(fp2,"%s", wordcheck)!=EOF)//Reads word from text file into array//
    {

        for (i=0; wordcheck[i]; i++)
        {
            wordcheck[i] = tolower(wordcheck[i]);//makes all characters lower case//
        }

        printf("%s", wordcheck);

        while(dictcount >= 0)//reads dictionary word into array//
        {   
            dictcount = 0;
            fscanf(fp1,"%s", worddict);

            if(strcmp(wordcheck, worddict)==0)//compare strings//
            {
            printf("This word: %s is in the dictionary
", wordcheck);
            break;
            }

            else
            {
            dictcount++;
            }

            if(worddict == NULL)
            {
            printf("Your word: %s is not in the dictionary
", wordcheck);
            }
        }
        dictcount++;
    }   
    fclose(fp1);
    fclose(fp2);

return 0;
}

推荐答案

通常的解决方法是先阅读字典,建立哈希表.然后,您将一次从输入文件中读取一个单词,如果哈希表中不存在该单词,则标记一个错误.

The usual way of solving this is to first read the dictionary and build a hash table. You'd then read one word at a time from the input file and flag an error if the word doesn't exist on the hash table.

这篇关于比较两个文本文件 - C 中的拼写检查程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆