Skip to content
  • P
    Projects
  • G
    Groups
  • S
    Snippets
  • Help

jobrod / Week3Programming2JordanBrodie

  • This project
    • Loading...
  • Sign in
Go to a project
  • Project
  • Repository
  • Issues 0
  • Merge Requests 0
  • Pipelines
  • Wiki
  • Snippets
  • Members
  • Activity
  • Graph
  • Charts
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
  • Files
  • Commits
  • Branches
  • Tags
  • Contributors
  • Graph
  • Compare
  • Charts
Commit 0bc31298 authored 3 months ago by jobrod's avatar jobrod
Browse files
Options
  • Browse Files
  • Download
  • Email Patches
  • Plain Diff

Add new file

parent fb3f53e7 master
Hide whitespace changes
Inline Side-by-side
Showing with 105 additions and 0 deletions
  • uniquewords.c
uniquewords.c 0 → 100644
View file @ 0bc31298
#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;
}
This diff is collapsed. Click to expand it.
  • Write
  • Preview
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