Count the occurrences of every word in C

209 Views Asked by At

I want to count the occurrences of every word of this small text " A broken heart of a broken mind ."

Every word of this text is in 2d array[100][20] in which 100 is the max_words and 20 is the max_word_length. And I have a pointers array[100] in which every pointer points the word. I can't find a clever way to count the same words, for example

a: 2 times
broken: 2 times
heart: 1 time
mind: 1 time
. : 1 time

These would be the pointers and the words array:

POINTERS ARRAY                      WORDS ARRAY
point0(points "a")                  a
point1(points "broken")             broken
point2(points "heart")              heart
point3(points "of")                 of
point4 (points "a")                 mind
point5(points "broken")             .
point6(points "mind")               \0\0\0\0\0
point7(points ".")                  \0\0\0\0\0
NULL                                ..
NULL
..
NULL                                \0\0\0\0\0

Side note: Every word is lowercase.

void frequence_word(char *pointers[], int frequence_array[]) {
  int word = 0;
  int i;
  int count = 1;
  int check[MAX_WORDS];

  for (word = 0; word < MAX_WORDS; word++) {
    check[word] = -1;
  }

  for (word = 0; word < MAX_WORDS; word++) {
    count = 1;

    for (i = word + 1; i < MAX_WORDS; i++) {

      if (pointers[word + 1] != NULL
          && strcmp(pointers[word], pointers[i]) == 0) {
        count++;
        check[i] = 0;
      }

    }
    if (check[word] != 0) {
      check[word] = count;
    }

  }
}

Any ideas please?

1

There are 1 best solutions below

0
entangled_photon On

This seems like a use case for strstr. You can call strstr, then iteratively reassign to the original string until NULL is reached.

const char substring[] = "A broken heart of a broken mind";
const char* total = ...;

const char* result;
long count = 0;
while (result = strstr(total, substring)) {
    count++;
    total += (sizeof(substring) - 1);
}

I think this is mostly self-explanetory, but I will explain this line:

total += (sizeof(substring) - 1);

It takes advantage of the fact that sizeof on arrays returns the array length. Thus, sizeof on a character array returns the number of characters in it. We subtract one to ignore the null terminator.