#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
*/