辞書とユーザーテキストファイルを読み込んでチェックする簡単なスペルチェッカーを作った。プログラムは、辞書にない単語の行と単語のインデックスを表示する必要があります。だから、ユーザーのテキストファイルが返されるまで\n
文字(段落または文の末尾に)が正しく機能します。だからこんにちは、実際にはHello\n
として辞書に対してテストされ、プログラムは間違っていると考えています。誰でも\n
文字を削除する方法をアドバイスできますか?ここに私のコードです:Cの n文字の呪い
#include <stdio.h>
#include <string.h>
void StrLower(char str[])
{
int i;
for (i = 0; str[i] != '\0'; i++)
str[i] = (char)tolower(str[i]);
}
int main (int argc, const char * argv[]) {
FILE *fpDict, *fpWords;
fpWords = fopen(argv[2], "r");
if((fpDict = fopen(argv[1], "r")) == NULL) {
printf("No dictionary file\n");
return 1;
}
char dictionaryWord[50]; // current word read from dictionary
char line[100]; // line read from spell check file (max 50 chars)
int isWordfound = 0; // 1 if word found in dictionary
int lineCount = 0; // line in spellcheck file we are currently on
int wordCount = 0; // word on line of spellcheck file we are currently on
while (fgets (line, sizeof line, fpWords) != NULL)
{
lineCount ++;
wordCount = 0;
char *spellCheckWord;
spellCheckWord = strtok(line, " ");
while (spellCheckWord != NULL) {
wordCount++;
spellCheckWord = strtok(NULL, " ,");
if(spellCheckWord==NULL)
continue;
StrLower(spellCheckWord);
printf("'%s'\n", spellCheckWord);
while(!feof(fpDict))
{
fscanf(fpDict,"%s",dictionaryWord);
int res = strcmp(dictionaryWord, spellCheckWord);
if(res==0)
{
isWordfound = 1;
break;
}
}
if(!isWordfound){
printf("word '%s' not found in Dictionary on line: %d, word index: %d\n", spellCheckWord, lineCount, wordCount); //print word and line not in dictionary
}
rewind(fpDict); //resets dictionarry file pointer
isWordfound = 0; //resets wordfound for next iteration
}
}
fclose(fpDict);
fclose(fpWords);
return 0;
}
すごくお返事ありがとうございました。あなたたちは素晴らしく、月の上にそれがあります!
これは質問の範囲外ですが、あなたのコードが間違っている:それはありません行の最初の単語をチェックしないでください。これは、単語のチェックの前に再度(strtokを使用して)トークン化しているためです。チェックの後、strtokが現在あるループの終わりにそれを行うべきです。 – Adonais