制作拼写检查器的问题,我不能删除标点符号

时间:2017-02-22 13:32:37

标签: c spell-checking punctuation

拼写检查器:

#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include "search.h"

int main(void)
{   
    FILE *f = fopen("mistakes.txt", "w");
    FILE *fPointer;
    FILE *dictionary;
    //int a,b;
    char fname[20];
    char wordcheck[45];
    char worddict[45];

    char dummy;
    int i;
    int notfound;
    int k;
  

这是我正在使用的文件,包括包含单词的字典文件   我比较其他文本文件

    ///^below add text file to be read
    fPointer = fopen("fname", "r");
    dictionary = fopen("dictionary.txt","r");   
if (f == NULL)
       {
        printf("Error oping file!\n");
        exit(1);
       }

我创建了这个循环,使单词更低,并识别标点符号

    while(fscanf(fPointer,"%s", wordcheck)!=EOF)// makes all characters lower case
    {      // an array that contains the characters of the original word
        char wordChars[1000];

        int newwordlength = 0;
            // loops through each character in a word
        for (i=0; i < strlen(wordcheck); i++)
         {
            // makes all words lower cased
            wordcheck[i] = tolower(wordcheck[i]);
            // checks the word has these characters and increments newwordlength where the character is a letter
              if (wordChars[i] != ',' 
              && wordChars[i] != '.' 
              && wordChars[i] != '?' 
              && wordChars[i] != '!' 
              && wordChars[i] != ':')
            {    // how long the word will be without these characters
                newwordlength++;
            }           
         } 
        //  creates new character array with size newwordlength
        char newWordChars[1000];
            *I think my problem is in this part*
        int currChar = 0;
  **This is where i think my problem is as its not removing the the characters **
        // loops through these characters and only adds non-punctuation characters to the new character array
        for (k = 0; k < strlen(wordChars); k++)
        {
            if (wordChars[k] != ','
                && wordChars[k] != '.'
                && wordChars[k] != '?'
                && wordChars[k] != '!'
                && wordChars[k] != ':')
              {
                newWordChars[currChar] = wordChars[k];
                currChar++;
              }
        }       

当我使用此循环时,我将错误复制到文本文件中,但它也会复制标点符号

        fseek(dictionary,0,SEEK_SET);

    while(fscanf(dictionary,"%s", worddict)!=EOF)
      {
        notfound = 1;
        if(strcmp(wordcheck, worddict)==0)// compares strings//
            {
              printf("This word: %s is in the dictionary\n", wordcheck);
              notfound = 0;

               break;
            }
      }
    if(notfound == 1)
  

这是复制txt文件的地方。

      {
        printf("%s is not in the dictionary\n", wordcheck);
        const char *mistakes = "Write this to file";
        fprintf(f, "Words not in the dictionary: %s\n", wordcheck);
      }
    printf("Your file has been checked. Please check your errors by typing mistakes.txt to view ");
    }

    fclose(dictionary);
    fclose(fPointer);

    return 0;
}

0 个答案:

没有答案