+/EE312
software design
git clone https://git.julia.blue/+/EE312
f830e85
master
1/* 2* SpellCheck <Project1.cpp> 3* EE w312 Project 1 submission by 4* Joseph Ryan 5* JBR2558 6* Slip days used: 0 7* Spring 2018 8*/ 9 10#include <ctype.h> 11#include <stdio.h> 12#include <stdlib.h> 13#include <string.h> 14 15#define MAX_DICT_SIZE 50000 16 17/* 18* Converts a string to all lowercase in place and returns a pointer to it. Any 19* non-uppercase characters are unmodified. 20*/ 21char * lowerCase (char * str ) { 22size_t len = strlen (str ); 23for (size_t i = 0 ;i < len ;++ i ) { 24if (str [i ] >='A' && str [i ] <='Z' ) { 25str [i ]+= 'a' - 'A' ; 26 } 27 } 28return str ; 29} 30 31/* 32* Parses the next "word" per the specifications of the article. On a first call 33* the function takes the string to be read, and in subsequent calls it expects 34* NULL. The function will automatically null terminate returned words (this 35* process is destructive so use a copy of the string if it's needed elsewhere). 36* When the end of the string is reached, returns NULL. 37*/ 38char * getNextWord (char * str ) { 39static char * s ; 40if (str != NULL ) { 41s = str ; 42 } 43// Find first alphabetic character to start a word. 44size_t i = 0 ; 45while (!isalpha (s [i ])) { 46if (s [i ]== '\0' ) { 47return NULL ; 48 } 49++ i ; 50 } 51s = & s [i ]; 52char * startOfWord = s ; 53char * endOfWord = s + 1 ; 54// If word is only one letter, skip it. 55if (!isalpha (* endOfWord )) { 56++ s ; 57return getNextWord (NULL ); 58 } 59// Find next non-alphabetic character to end the word. 60while (isalpha (* endOfWord )) { 61++ endOfWord ; 62 } 63// Only advance the pointer to next word if not already at end of string. 64s = endOfWord + (* endOfWord != '\0' ); 65* endOfWord = '\0' ; 66return startOfWord ; 67} 68 69/* 70* Destructively converts the given string of newline terminated words to an array 71* of null terminated strings. Returns the resulting array (must be freed). 72*/ 73char ** parseDict (char * raw ) { 74char delim []= "\n" ; 75char ** dict = (char ** )malloc (MAX_DICT_SIZE * sizeof (char * )); 76char * entry ; 77size_t i = 0 ; 78entry = strtok (raw ,delim ); 79while (entry != NULL ) { 80dict [i ]= lowerCase (entry ); 81++ i ; 82entry = strtok (NULL ,delim ); 83 } 84dict [i ]= NULL ; 85return dict ; 86} 87 88/* 89* Returns true if the given word is in the given dictionary, otherwise false. 90*/ 91int inDict (char * word ,char ** dict ) { 92size_t i = 0 ; 93while (dict [i ]!= NULL ) { 94if (!strcmp (word ,dict [i ])) { 95return 1 ; 96 } 97++ i ; 98 } 99return 0 ; 100} 101 102/* 103* Displays the members of a dictionary for debugging purposes. 104*/ 105void printDict (char ** d ) { 106for (size_t i = 0 ;d [i ]!= NULL ;++ i ) { 107printf ("%s\n" ,d [i ]); 108 } 109} 110 111/* 112* Displays the contents of an article for debugging purposes. 113*/ 114void printArticle (char * a ) { 115char * temp = strdup (a );// Make copy since getNextWord() is destructive. 116char * word = getNextWord (temp ); 117if (word == NULL ) { 118return ; 119 } 120do { 121printf ("%s\n" ,word ); 122 }while (word = getNextWord (NULL )); 123} 124 125/* 126* Prints each word in the given article that is not found in the given dictionary. 127*/ 128void spellCheck (char * article ,char * dictionary ) { 129char ** dict = parseDict (dictionary ); 130char * word = getNextWord (article ); 131if (word == NULL ) { 132return ; 133 } 134do { 135if (!inDict (lowerCase (word ),dict )) { 136printf ("%s\n" ,word ); 137 } 138 }while (word = getNextWord (NULL )); 139} 140