hope everyone is well! im a bit stuck on my programming assignment, the purpose of the c code is to compare a txt file (name given as user input) to another keywords txt file. then record the frequency of each word and print them in descending order. This should look like this:
euery 8
common 8
gaue 7
thankes 5
vnkle 4
growes 3
wag 3
seal 3
day 3
soft 3
sadly this isnt what my code produces. the code is as shown below:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_FILENAME_LENGTH 256
#define MAX_TEXT_LENGTH 100000
#define MAX_KEYWORDS 100
// Structure to store keyword frequencies
typedef struct KeywordFrequency {
char keyword[50];
int frequency;
} KeywordFrequency;
// Compare function for sorting keyword frequencies
int compareKeywords(const void *a, const void *b) {
return ((KeywordFrequency *)b)->frequency - ((KeywordFrequency *)a)->frequency;
}
int main() {
char filename[MAX_FILENAME_LENGTH];
char text[MAX_TEXT_LENGTH];
KeywordFrequency keywords[MAX_KEYWORDS];
int totalKeywords = 0;
// Ask the user for the name of the text file
printf("Enter the name of the text file: ");
scanf("%s", filename);
// Open and read the text file
FILE *textFile = fopen(filename, "r");
if (textFile == NULL) {
printf("Error opening %s\n", filename);
return 1;
}
// Read the content of the text file
fread(text, 1, sizeof(text), textFile);
fclose(textFile);
// Open and read the keyword file
FILE *keywordFile = fopen("keyword.txt", "r");
if (keywordFile == NULL) {
printf("Error opening keyword.txt\n");
return 1;
}
// Initialize keyword frequencies
while (fscanf(keywordFile, "%s", keywords[totalKeywords].keyword) != EOF) {
keywords[totalKeywords].frequency = 0;
totalKeywords++;
}
fclose(keywordFile);
// Tokenize and compare the text with keywords
char *token = strtok(text, " ");
while (token != NULL) {
for (int i = 0; i < totalKeywords; i++) {
if (strcmp(token, keywords[i].keyword) == 0) {
keywords[i].frequency++;
}
}
token = strtok(NULL, " ");
}
// Sort keyword frequencies in descending order
qsort(keywords, totalKeywords, sizeof(KeywordFrequency), compareKeywords);
// Print the results in a table format
printf("Keyword\tFrequency\n");
for (int i = 0; i < totalKeywords; i++) {
printf("%s\t%d\n", keywords[i].keyword, keywords[i].frequency);
}
return 0;
}
any help would be greatly appreciated
Two things:
The first is that the
scanfformat%sreads space delimited words. So there's nothing to tokenize. You never even read anything intotextwhich you attempt to tokenize;The second thing is that you read every single word of the input file into a separate "keyword" element, which is not what you should do. And will go out of bounds of the array if there's more than 100 words in the input file.
Instead you should read into another variable and search the current keywords, and find if it's in the array or not, and if it is then increase the frequency, else add it.