Commit 73fa8c35 by jobrod

Update uniquewords.c

parent 844156f3
Showing with 18 additions and 1 deletions
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <ctype.h> #include <ctype.h>
...@@ -17,6 +16,7 @@ typedef struct { ...@@ -17,6 +16,7 @@ typedef struct {
void process_file(const char *filename); void process_file(const char *filename);
int count_unique_words(char *line, WordCount *word_counts, int *unique_word_count); 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 find_word(WordCount *word_counts, int unique_word_count, const char *word);
void remove_punctuation(char *line);
int main() int main()
{ {
...@@ -45,6 +45,7 @@ void process_file(const char *filename) ...@@ -45,6 +45,7 @@ void process_file(const char *filename)
while (fgets(line, sizeof(line), file)) while (fgets(line, sizeof(line), file))
{ {
remove_punctuation(line);
count_unique_words(line, word_counts, &unique_word_count); count_unique_words(line, word_counts, &unique_word_count);
} }
...@@ -53,6 +54,21 @@ void process_file(const char *filename) ...@@ -53,6 +54,21 @@ void process_file(const char *filename)
printf("There are %d different words in this text.\n", unique_word_count); printf("There are %d different words in this text.\n", unique_word_count);
} }
// Function to remove punctuation from a line
void remove_punctuation(char *line)
{
char *src = line, *dst = line;
while (*src)
{
if (!ispunct((unsigned char)*src))
{
*dst++ = *src;
}
src++;
}
*dst = '\0';
}
// Function to count unique words in a line // Function to count unique words in a line
int count_unique_words(char *line, WordCount *word_counts, int *unique_word_count) int count_unique_words(char *line, WordCount *word_counts, int *unique_word_count)
{ {
...@@ -103,3 +119,4 @@ int find_word(WordCount *word_counts, int unique_word_count, const char *word) ...@@ -103,3 +119,4 @@ int find_word(WordCount *word_counts, int unique_word_count, const char *word)
} }
return -1; 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