Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,880 questions

51,806 answers

573 users

How to count occurrences of each word in a text file with C

1 Answer

0 votes
#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#define MAX_WORD_LENGTH 100
#define INITIAL_CAPACITY 10

typedef struct {
    char word[MAX_WORD_LENGTH];
    int count;
} WordCount;

void toLowerCase(char* str) {
    for (int i = 0; str[i]; i++) {
        str[i] = tolower(str[i]);
    }
}

int findWord(WordCount* wordCounts, int size, const char* word) {
    for (int i = 0; i < size; i++) {
        if (strcmp(wordCounts[i].word, word) == 0) {
            return i;
        }
    }
    return -1;
}

void addWord(WordCount** wordCounts, int* size, int* capacity, const char* word) {
    int index = findWord(*wordCounts, *size, word);
    if (index != -1) {
        (*wordCounts)[index].count++;
    }
    else {
        if (*size >= *capacity) {
            *capacity *= 2;
            *wordCounts = realloc(*wordCounts, *capacity * sizeof(WordCount));
            if (wordCounts == NULL) {
                puts("realloc error");
                return;
            }
        }
        strcpy((*wordCounts)[*size].word, word);
        (*wordCounts)[*size].count = 1;
        (*size)++;
    }
}

void countWordsInFile(const char* filename) {
    FILE* file = fopen(filename, "r");
    if (!file) {
        perror("Could not open file");
        return;
    }

    WordCount* wordCounts = malloc(INITIAL_CAPACITY * sizeof(WordCount));
    if (wordCounts == NULL) {
        puts("malloc error");
        return;
    }
    int size = 0, capacity = INITIAL_CAPACITY;
    char word[MAX_WORD_LENGTH];

    while (fscanf(file, "%99s", word) == 1) {
        toLowerCase(word);
        addWord(&wordCounts, &size, &capacity, word);
    }

    fclose(file);

    for (int i = 0; i < size; i++) {
        printf("%s: %d\n", wordCounts[i].word, wordCounts[i].count);
    }

    free(wordCounts);
}

int main() {
    const char* filename = "d:\\data.txt";

    countWordsInFile(filename);

    return 0;
}



/*
run:

c: 2
is: 2
a: 1
general-purpose: 1
programming: 1
language.: 1
it: 2
was: 1
created: 1
in: 3
the: 5
1970s: 1
by: 2
dennis: 1
ritchie: 1
and: 4
remains: 1
very: 1
widely: 1
used: 2
influential.: 1
design,: 1
c's: 1
features: 1
cleanly: 1
reflect: 1
capabilities: 1
of: 1
targeted: 1
cpus.: 1
has: 2
found: 1
lasting: 1
use: 2
operating: 1
systems: 1
code: 1
device: 1
drivers,: 1
but: 1
its: 1
application: 1
software: 1
been: 1
decreasing.: 1
commonly: 1
on: 1
computer: 1
architectures: 1
that: 1
range: 1
from: 1
largest: 1
supercomputers: 1
to: 1
smallest: 1
microcontrollers: 1
embedded: 1
systems.: 1

*/

 



answered Mar 1, 2025 by avibootz

Related questions

1 answer 224 views
1 answer 162 views
1 answer 173 views
1 answer 170 views
1 answer 165 views
1 answer 161 views
1 answer 162 views
...