Add c/word-count

This commit is contained in:
dwrz
2019-03-17 03:13:13 +00:00
parent cd8e5aaea6
commit 6f0dc97c53
8 changed files with 3039 additions and 0 deletions
+82
View File
@@ -0,0 +1,82 @@
#include <ctype.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include "word_count.h"
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)++;
}
}
bool increment_seen_words(char *token,
int unique_words,
word_count_word_t *words) {
for (int i = 0; i < unique_words; i++) {
if (strcmp(token, words[i].text) == 0) {
words[i].count++;
return true;
}
}
return false;
}
bool is_quoted_word(char *token) {
return (is_quote_char(token[0]) &&
is_quote_char(token[strlen(token)-1]));
}
bool is_quote_char(char c) {
switch (c) {
case '\'':
return true;
break;
case '"':
return true;
break;
}
return false;
}
void lowercase(char *s) {
for(int i = 0; s[i]; i++){
s[i] = tolower(s[i]);
}
}
+41
View File
@@ -0,0 +1,41 @@
#ifndef WORD_COUNT_H
#include <stdbool.h>
#define WORD_COUNT_H
#define MAX_WORDS 20 // at most MAX_WORDS can be found in the test input string
#define MAX_WORD_LENGTH 50 // no individual word can exceed this length
// results structure
typedef struct word_count_word {
char text[MAX_WORD_LENGTH + 1]; // allow for the string to be null-terminated
int count;
} word_count_word_t;
#define EXCESSIVE_LENGTH_WORD -1
#define EXCESSIVE_NUMBER_OF_WORDS -2
// word_count - routine to classify the unique words and their frequency in a test input string
// inputs:
// input_text = a null-terminated string containing that is analyzed
//
// outputs:
// words = allocated structure to record the words found and their frequency
// uniqueWords - number of words in the words structure
// returns a negative number if an error.
// words will contain the results up to that point.
int word_count(const char *input_text, word_count_word_t *words);
bool increment_seen_words(char *word, int count, word_count_word_t *origin);
void lowercase(char *s);
bool is_quote_char(char s);
bool is_quoted_word(char *token);
void process_token(int *unique_words,
char *token,
word_count_word_t *words);
#endif