Sha256: 13a3dad988dda97a771ba61c45f374a07ab83f235810b3937c0071b8bd3774af

Contents?: true

Size: 1.86 KB

Versions: 247

Compression:

Stored size: 1.86 KB

Contents

#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "word_count.h"

// checks to see if we are counting a given word:
// if word counted already it returns index,
// else returns -1
static int word_match(char *testWord, int current_word_count,
                      WordCount_Word_t words[MAX_WORDS])
{
   int matchIndex = -1;

   for (int index = 0; index < current_word_count; index++) {
      if (!strcmp(testWord, words[index].text)) {
         matchIndex = index;
         break;
      }
   }
   return matchIndex;
}

int word_count(char *input_text, WordCount_Word_t words[MAX_WORDS])
{
   int index;
   int uniqueWords = 0;
   char *testWord;
   char *lowerCaseInputText;    // hold lowercase copy of passed text
   const char *delimiters = " ,.-\n:!!&@$%^&\"";

   // convert the input text to lower case
   lowerCaseInputText = malloc(strlen(input_text) + 1);
   for (index = 0; input_text[index] != 0; index++) {
      lowerCaseInputText[index] = tolower(input_text[index]);
   }
   lowerCaseInputText[index] = '\0';

   // start with known results...
   memset(words, 0, sizeof(WordCount_Word_t));

   testWord = strtok(lowerCaseInputText, delimiters);
   while (testWord != NULL) {
      // account for leading/trailing single quote.
      if (('\'' == testWord[0]) && ('\'' == testWord[strlen(testWord) - 1])) {
         testWord[strlen(testWord) - 1] = '\0';
         testWord++;
      }
      // determine index of counted word.
      index = word_match(testWord, uniqueWords, words);

      // add if not yet counted else increment count.
      if (-1 == index) {
         words[uniqueWords].count = 1;
         strcpy(words[uniqueWords].text, testWord);
         uniqueWords++;
      } else {
         words[index].count++;
      }
      // get next word in string
      testWord = strtok(NULL, delimiters);
   }

   free(lowerCaseInputText);
   return uniqueWords;
}

Version data entries

247 entries across 247 versions & 1 rubygems

Version Path
trackler-2.2.1.45 tracks/c/exercises/word-count/src/example.c
trackler-2.2.1.44 tracks/c/exercises/word-count/src/example.c
trackler-2.2.1.43 tracks/c/exercises/word-count/src/example.c
trackler-2.2.1.42 tracks/c/exercises/word-count/src/example.c
trackler-2.2.1.41 tracks/c/exercises/word-count/src/example.c
trackler-2.2.1.40 tracks/c/exercises/word-count/src/example.c
trackler-2.2.1.39 tracks/c/exercises/word-count/src/example.c
trackler-2.2.1.38 tracks/c/exercises/word-count/src/example.c
trackler-2.2.1.37 tracks/c/exercises/word-count/src/example.c
trackler-2.2.1.36 tracks/c/exercises/word-count/src/example.c
trackler-2.2.1.35 tracks/c/exercises/word-count/src/example.c
trackler-2.2.1.34 tracks/c/exercises/word-count/src/example.c
trackler-2.2.1.33 tracks/c/exercises/word-count/src/example.c
trackler-2.2.1.32 tracks/c/exercises/word-count/src/example.c
trackler-2.2.1.31 tracks/c/exercises/word-count/src/example.c
trackler-2.2.1.30 tracks/c/exercises/word-count/src/example.c
trackler-2.2.1.29 tracks/c/exercises/word-count/src/example.c
trackler-2.2.1.28 tracks/c/exercises/word-count/src/example.c
trackler-2.2.1.27 tracks/c/exercises/word-count/src/example.c
trackler-2.2.1.26 tracks/c/exercises/word-count/src/example.c