Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

string - Compare two text files - spellchecking program in C

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!

EDIT: 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;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...