您好我正在c中製作一個拼寫檢查器,它有一個字符串數組中的字典,並使用二進制搜索來查找字典中的單詞。從沒有標點符號的文件中獲取字符串,以便用原始標點符號進行拼寫檢查輸出。
我的問題是,我正在嘗試從文件中讀取文本,並將文本輸出回新文件,並使用如下所示的錯誤詞語:**拼寫錯誤**,但該文件將包含諸如。,!之類的字符。應將其輸出到新文件中,但在將該單詞與字典進行比較時顯然不存在。
,所以我想這一點:
text file: "worng!"
new file: "** worng **!"
我一直在試圖解決這個問題的最好我能和花了相當長的一段對谷歌,但我沒有得到任何接近的解決方案。到目前爲止,我已經編寫了下面的代碼來讀取每個字符,並填充兩個char數組作爲字典比較的一個小寫字母的temp和一個原始單詞的輸入,如果沒有標點符號,則起作用,但顯然當標點符號存在時,我會以這種方式釋放空間I肯定有更好的方法來做到這一點,但我不能找到它,所以任何指針將不勝感激。
#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);
}
真的不知道爲什麼這已經否決了我花了這個2天,嘗試了不同的方法但是很好,真的卡住了。我也在多個網站上搜索了一個解決方案,包括這個,我現在的代碼是我能夠用我的研究做的。我想提醒大家,我是一名學生,並且只學習了大約9周的時間,因此向更有經驗的人士尋求指導是很自然的。 – Astabh 2012-04-04 19:37:05