嗨,我正在用 c 语言制作一个拼写检查器,它在字符串数组中有一个字典,并使用二进制搜索在字典中查找单词。
我的问题是我正在尝试从文件中读取文本并将文本输出回新文件,其中错误的单词突出显示如下:** 拼写错误 ** 但该文件将包含诸如 .,!? 它应该输出到新文件,但在将单词与字典进行比较时显然不存在。
所以我想要这个:
text file: "worng!"
new file: "** worng **!"
我一直在尽我所能解决这个问题,并且在谷歌上花了很长时间,但离解决方案还差得远。到目前为止,我已经编写了以下代码来读取每个字符并填充两个 char 数组一个小写临时用于字典比较,一个输入用于原始单词,如果没有标点符号则可以使用,但显然当标点符号存在时我会以这种方式释放空间我'确定有更好的方法可以做到这一点,但我找不到它,所以任何指针都会受到赞赏。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define MAX_STRING_SIZE 29 /*define longest non-technical word in english dictionary plus 1*/
/*function prototypes*/
int dictWordCount(FILE *ptrF); /*counts and returns number of words in dictionary*/
void loadDictionary(char ***pArray1, FILE *ptrFile, int counter); /*create dictionary array from file based on word count*/
void printDictionary(char **pArray2, int size); /*prints the words in the dictionary*/
int binarySearch(char **pArray3, int low, int high, char *value); /*recursive binary search on char array*/
void main(int argc, char *argv[]){
int i; /*index*/
FILE *pFile; /*pointer to dictionary file*/
FILE *pInFile; /*pointer to text input file*/
FILE *pOutFile; /*pointer to text output file*/
char **dict; /*pointer to array of char pointer - dictionary*/
int count; /*number of words in dictionary*/
int dictElement; /*element the word has been found at returns -1 if word not found*/
char input[MAX_STRING_SIZE]; /*input to find in dictionary*/
char temp[MAX_STRING_SIZE];
char ch; /*store each char as read - checking for punctuation or space*/
int numChar = 0; /*number of char in input string*/
/*************************************************************************************************/
/*open dictionary file*/
pFile = fopen("dictionary.txt", "r"); /*open file dictionary.txt for reading*/
if(pFile==NULL){ /*if file can't be opened*/
printf("ERROR: File could not be opened!/n");
exit(EXIT_FAILURE);
}
count = dictWordCount(pFile);
printf("Number of words is: %d\n", count);
/*Load Dictionary into array*/
loadDictionary(&dict, pFile, count);
/*print dictionary*/
//printDictionary(dict, count);
/*************************************************************************************************/
/*open input file for reading*/
pInFile = fopen(argv[1], "r");
if(pInFile==NULL){ /*if file can't be opened*/
printf("ERROR: File %s could not be opened!/n", argv[1]);
exit(EXIT_FAILURE);
}
/*open output file for writing*/
pOutFile = fopen(argv[2], "w");
if(pOutFile==NULL){ /*if file can't be opened*/
printf("ERROR: File could not be created!/n");
exit(EXIT_FAILURE);
}
do{
ch = fgetc(pInFile); /*read char fom file*/
if(isalpha((unsigned char)ch)){ /*if char is alphabetical char*/
//printf("char is: %c\n", ch);
input[numChar] = ch; /*put char into input array*/
temp[numChar] = tolower(ch); /*put char in temp in lowercase for dictionary check*/
numChar++; /*increment char array element counter*/
}
else{
if(numChar != 0){
input[numChar] = '\0'; /*add end of string char*/
temp[numChar] = '\0';
dictElement = binarySearch(dict,0,count-1,temp); /*check if word is in dictionary*/
if(dictElement == -1){ /*word not in dictionary*/
fprintf(pOutFile,"**%s**%c", input, ch);
}
else{ /*word is in dictionary*/
fprintf(pOutFile, "%s%c", input, ch);
}
numChar = 0; /*reset numChar for next word*/
}
}
}while(ch != EOF);
/*******************************************************************************************/
/*free allocated memory*/
for(i=0;i<count;i++){
free(dict[i]);
}
free(dict);
/*close files*/
fclose(pInFile);
fclose(pOutFile);
}