+/EE312

software design

git clone https://git.julia.blue/+/EE312

Joseph RyanAdd Project 1 and start Project 2f830e85

master
3.5 KiB140 linesraw
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) {
22    size_t len = strlen(str);
23    for(size_t i = 0; i < len; ++i) {
24        if(str[i] >= 'A' && str[i] <= 'Z') {
25            str[i] += 'a' - 'A';
26        }
27    }
28    return 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) {
39    static char* s;
40    if(str != NULL) {
41        s = str;
42    }
43    // Find first alphabetic character to start a word.
44    size_t i = 0;
45    while(!isalpha(s[i])) {
46        if(s[i] == '\0') {
47            return NULL;
48        }
49        ++i;
50    }
51    s = &s[i];
52    char* startOfWord = s;
53    char* endOfWord = s + 1;
54    // If word is only one letter, skip it.
55    if(!isalpha(*endOfWord)) {
56        ++s;
57        return getNextWord(NULL);
58    }
59    // Find next non-alphabetic character to end the word.
60    while(isalpha(*endOfWord)) {
61        ++endOfWord;
62    }
63    // Only advance the pointer to next word if not already at end of string.
64    s = endOfWord + (*endOfWord != '\0');
65    *endOfWord = '\0';
66    return 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) {
74    char delim[] = "\n";
75    char** dict = (char**)malloc(MAX_DICT_SIZE * sizeof(char*));
76    char* entry;
77    size_t i = 0;
78    entry = strtok(raw, delim);
79    while(entry != NULL) {
80        dict[i] = lowerCase(entry);
81        ++i;
82        entry = strtok(NULL, delim);
83    }
84    dict[i] = NULL;
85    return 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) {
92    size_t i = 0;
93    while(dict[i] != NULL) {
94        if(!strcmp(word, dict[i])) {
95            return 1;
96        }
97        ++i;
98    }
99    return 0;
100}
101
102/*
103 * Displays the members of a dictionary for debugging purposes.
104 */
105void printDict(char** d) {
106    for(size_t i = 0; d[i] != NULL; ++i) {
107        printf("%s\n", d[i]);
108    }
109}
110
111/*
112 * Displays the contents of an article for debugging purposes.
113 */
114void printArticle(char* a) {
115    char* temp = strdup(a); // Make copy since getNextWord() is destructive.
116    char* word = getNextWord(temp);
117    if(word == NULL) {
118        return;
119    }
120    do {
121        printf("%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) {
129    char** dict = parseDict(dictionary);
130    char* word = getNextWord(article);
131    if(word == NULL) {
132        return;
133    }
134    do {
135        if(!inDict(lowerCase(word), dict)) {
136            printf("%s\n", word);
137        }
138    } while(word = getNextWord(NULL));
139}
140