Commit 0bc31298 by jobrod

Add new file

parent fb3f53e7
Showing with 105 additions and 0 deletions
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define MAX_LINE 1024
#define MAX_WORDS 50000 // Increased limit for unique words
#define MAX_WORD_LENGTH 100
// Structure to store unique words and their counts
typedef struct {
char word[MAX_WORD_LENGTH];
int count;
} WordCount;
// Function prototypes
void process_file(const char *filename);
int count_unique_words(char *line, WordCount *word_counts, int *unique_word_count);
int find_word(WordCount *word_counts, int unique_word_count, const char *word);
int main()
{
process_file("raamat1184.txt");
return 0;
}
// Function to process the file and count unique words
void process_file(const char *filename)
{
FILE *file = fopen(filename, "r");
if (file == NULL)
{
printf("Error: Could not open file.\n");
return;
}
else
{
printf("File opened successfully.\n");
}
char line[MAX_LINE];
int unique_word_count = 0;
WordCount word_counts[MAX_WORDS] = {0}; // Array to store unique words and their counts
while (fgets(line, sizeof(line), file))
{
count_unique_words(line, word_counts, &unique_word_count);
}
fclose(file);
printf("File closed successfully.\n");
printf("There are %d different words in this text.\n", unique_word_count);
}
// Function to count unique words in a line
int count_unique_words(char *line, WordCount *word_counts, int *unique_word_count)
{
char *word = strtok(line, " \t\n");
while (word != NULL)
{
// Convert word to lowercase to ensure case-insensitive comparison
for (char *p = word; *p; ++p) *p = tolower(*p);
int index = find_word(word_counts, *unique_word_count, word);
if (index == -1)
{
// New unique word
if (*unique_word_count < MAX_WORDS)
{
strncpy(word_counts[*unique_word_count].word, word, MAX_WORD_LENGTH - 1);
word_counts[*unique_word_count].word[MAX_WORD_LENGTH - 1] = '\0'; // Ensure null-termination
word_counts[*unique_word_count].count = 1;
(*unique_word_count)++;
}
else
{
printf("Error: Exceeded maximum number of unique words.\n");
return -1;
}
}
else
{
// Existing word
word_counts[index].count++;
}
word = strtok(NULL, " \t\n");
}
return *unique_word_count;
}
// Function to find a word in the word_counts array
int find_word(WordCount *word_counts, int unique_word_count, const char *word)
{
for (int i = 0; i < unique_word_count; i++)
{
if (strcmp(word_counts[i].word, word) == 0)
{
return i;
}
}
return -1;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment