Note that there are some explanatory texts on larger screens.

plurals
  1. POgetting string from file without punctuation for spell checking outputting with original punctuation.
    text
    copied!<p>Hi I'm making a spell checker in c that has a dictionary in an array of strings and uses binary search to find words in dictionary.</p> <p>My problem is that I am trying to read text from a file and output the text back to a new file with wrong words highlighted like this: ** spellingmistake ** but the file will include characters such as .,!? which should be output to the new file but obviously not be present when comparing the word to the dictionary. </p> <p>so I want this:</p> <pre><code>text file: "worng!" new file: "** worng **!" </code></pre> <p>I've been trying to solve this the best I can and have spent quite a while on google, but am not getting any closer to a solution. I have written the following code so far to read each character and fill two char arrays one lower case temp for dictionary comparison and one input for original word which works if there is no punctuation but obviously I loose the space this way when punctuation is present I'm sure there is a better way to do this but I just can't find it so any pointers would be appreciated.</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;string.h&gt; #include &lt;ctype.h&gt; #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(&amp;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&lt;count;i++){ free(dict[i]); } free(dict); /*close files*/ fclose(pInFile); fclose(pOutFile); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload