Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // a SLOW spell checker
- #include <ctype.h>
- #include <stdio.h>
- #include <string.h>
- #include "dictionary.h"
- // remove non-alphabetic characters and convert to lower case
- void trimWord(char *word) {
- int k = 0;
- for (int i = 0; i < (int) strlen(word); i++) {
- if (isalpha(word[i])) {
- word[k] = tolower(word[i]);
- k++;
- }
- }
- word[k] = '\0';
- }
- int main(int argc, char *argv[]) {
- char word[LENGTH + 1];
- memset(word, 0, sizeof(char)*LENGTH+1);
- // step 1: read in the dictionary
- struct Trie *dictionary = emptyTrieNode();
- while (scanf("%45s",word) && word[0] != '!') {
- trimWord(word);
- addWord(dictionary,word);
- }
- memset(word, 0, sizeof(char)*LENGTH+1);
- // step 2: read in text
- int counter = 0; // number of unknown words
- int index = 0; // index of the string that we use to store the words
- int c = EOF;
- c=getchar(); // we skip the first end of line
- while ((c = getchar()) && c != EOF) { // main loop for reading the words.
- if(c-','==0 || c-'\n'==0 || c-'.'==0 || c-'\''==0 || // We check for all the symbols that denote the boundaries of the word
- c-'/'==0 || c-'-'==0 || c-'\"'==0 || c-':'==0 ||
- c-';'==0 || c-'?'==0 || c-'!'==0 || c-'('==0 ||
- c-')'==0 || (int)c==0 || (int)c==1 || (int)c==2 || (int)c==3 || (int)c==4||
- (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.
- trimWord(word); // we convert to lowercase
- if(!check(dictionary,word) && index!=0){ // when the word is not in the dictionary we output it and increment the counter
- counter++;
- printf("%s\n",word);
- }
- memset(word, 0, sizeof(char)*LENGTH+1); // we make sure to clear the string after checking the word
- index=0; // also reseting the index
- } else { // if we encounter a letter we add it in the string and increment the index.
- word[index]=c;
- index++;
- }
- }
- // step 3: print number of unknown words
- printf("%d\n", counter);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement