Advertisement
Guest User

Untitled

a guest
Feb 26th, 2020
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.20 KB | None | 0 0
  1. // a fast spell checker
  2.  
  3. #include <ctype.h>
  4. #include <stdio.h>
  5. #include <string.h>
  6.  
  7. #include "dictionary.h"
  8.  
  9.  
  10.  
  11.  
  12. // remove non-alphabetic characters and convert to lower case
  13. void trimWord(char *word) {
  14.   int k = 0;
  15.   for (int i = 0; i < (int) strlen(word); i++) {
  16.     if (isalpha(word[i])) {
  17.       word[k] = tolower(word[i]);
  18.       k++;
  19.     }
  20.   }
  21.   word[k] = '\0';
  22. }
  23.  
  24. int main(int argc, char *argv[]) {
  25.  
  26.   char word[LENGTH + 1];
  27.   memset(word, 0, sizeof(char)*LENGTH+1);
  28.  
  29.   // step 1: read in the dictionary
  30.   struct Trie *dictionary = emptyTrieNode();
  31.   while (scanf("%45s",word) && word[0] != '!') {
  32.     trimWord(word);
  33.     addWord(dictionary,word);
  34.   }
  35.   memset(word, 0, sizeof(char)*LENGTH+1);
  36.  
  37.   // step 2: read in text
  38.   int counter = 0; // number of unknown words
  39.   int index = 0;  //  index of the string that we use to store the words
  40.   int c = EOF;
  41.   c=getchar();    // we skip the first end of line
  42.   while ((c = getchar()) && c != EOF) {           // main loop for reading the words.
  43.     if(c-','==0 || c-'\n'==0 || c-'.'==0 || c-'\''==0 ||                          // We check for all the symbols that denote the boundaries of the word
  44.        c-'/'==0 || c-'-'==0 || c-'\"'==0 || c-':'==0 ||
  45.        c-';'==0 || c-'?'==0 || c-'!'==0 || c-'('==0 ||
  46.        c-')'==0 || (int)c==0 || (int)c==1 || (int)c==2 || (int)c==3 || (int)c==4||
  47.        (int)c==5 || (int)c==6 || (int)c==7 || (int)c==8 || (int)c==9 || c==' '){  //  This if looks a bit ugly, but we used this layout so it's more readable.
  48.       trimWord(word); // we convert to lowercase
  49.       if(!check(dictionary,word) && index!=0){    // when the word is not in the dictionary we output it and increment the counter
  50.         counter++;
  51.         printf("%s\n",word);
  52.       }
  53.       memset(word, 0, sizeof(char)*LENGTH+1);     // we make sure to clear the string after checking the word
  54.       index=0;                                    // also reseting the index
  55.     } else {                                      // if we encounter a letter we add it in the string and increment the index.
  56.       word[index]=c;
  57.       index++;
  58.     }
  59.   }
  60.  
  61.  
  62.   // step 3: print number of unknown words
  63.   printf("%d\n", counter);
  64.  
  65.   return 0;
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement