Refactor word_count.c

Make internal functions static, reorder for simultaneous declaration.

Reformat whitespace for readability.
This commit is contained in:
dwrz
2019-04-06 20:32:11 +00:00
parent 403aebe443
commit 363cd3ab14
+60 -52
View File
@@ -7,46 +7,28 @@
const char *DELIMITERS = " ..,\n:!@#$%^&*()[]{}";
int word_count(const char *input_text, word_count_word_t *words) {
int unique_words = 0;
// Zero out the buffer; otherwise values will persist across invocations.
memset(words, 0, sizeof(word_count_word_t) * MAX_WORDS);
// strtok modifies strings. We'll use a copy, which we'll lowercase.
char copy_text[strlen(input_text)];
strcpy(copy_text, input_text);
lowercase(copy_text);
// Main loop -- tokenize and process copy_text.
for (char *token = strtok(copy_text, DELIMITERS);
token != NULL;
token = strtok(NULL, DELIMITERS)) {
if (unique_words > MAX_WORDS) {
return EXCESSIVE_NUMBER_OF_WORDS;
}
if (strlen(token) > MAX_WORD_LENGTH) {
return EXCESSIVE_LENGTH_WORD;
}
process_token(&unique_words, token, words);
}
return unique_words;
}
void process_token(int *unique_words,
char *token,
word_count_word_t *words) {
while (is_quoted_word(token)) { // Strip the quotes from the string.
token++; // Start at the char after the quote.
token[strlen(token)-1] = '\0'; // Terminate at the last quote.
}
bool found = increment_seen_words(token, *unique_words, words);
if (!found) {
words += (*unique_words);
strcpy(words -> text, token);
words -> count = 1;
(*unique_words)++;
static void lowercase(char *s) {
for(int i = 0; s[i]; i++){
s[i] = tolower(s[i]);
}
}
bool increment_seen_words(char *token,
static bool is_quote_char(char c) {
switch (c) {
case '\'':
return true;
case '"':
return true;
}
return false;
}
static bool is_quoted_word(char *token) {
return (is_quote_char(token[0]) &&
is_quote_char(token[strlen(token)-1]));
}
static bool increment_seen_words(char *token,
int unique_words,
word_count_word_t *words) {
for (int i = 0; i < unique_words; i++) {
@@ -58,23 +40,49 @@ bool increment_seen_words(char *token,
return false;
}
bool is_quoted_word(char *token) {
return (is_quote_char(token[0]) &&
is_quote_char(token[strlen(token)-1]));
static void process_token(int *unique_words,
char *token,
word_count_word_t *words) {
while (is_quoted_word(token)) { // Strip the quotes from the string.
token++; // Start at the char after the quote.
token[strlen(token)-1] = '\0'; // Terminate at the last quote.
}
bool found = increment_seen_words(token, *unique_words, words);
if (!found) {
words += (*unique_words);
strcpy(words -> text, token);
words -> count = 1;
(*unique_words)++;
}
}
bool is_quote_char(char c) {
switch (c) {
case '\'':
return true;
case '"':
return true;
}
return false;
}
int word_count(const char *input_text, word_count_word_t *words) {
int unique_words = 0;
void lowercase(char *s) {
for(int i = 0; s[i]; i++){
s[i] = tolower(s[i]);
// Zero out the buffer; otherwise values will persist across invocations.
memset(words, 0, sizeof(word_count_word_t) * MAX_WORDS);
// strtok modifies strings. We'll use a copy, which we'll lowercase.
char copy_text[strlen(input_text)];
strcpy(copy_text, input_text);
lowercase(copy_text);
// Main loop -- tokenize and process copy_text.
for (char *token = strtok(copy_text, DELIMITERS);
token != NULL;
token = strtok(NULL, DELIMITERS)) {
if (unique_words >= MAX_WORDS) {
return EXCESSIVE_NUMBER_OF_WORDS;
}
if (strlen(token) > MAX_WORD_LENGTH) {
return EXCESSIVE_LENGTH_WORD;
}
process_token(&unique_words, token, words);
}
return unique_words;
}