-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwordcount.c
More file actions
152 lines (139 loc) · 4.87 KB
/
Copy pathwordcount.c
File metadata and controls
152 lines (139 loc) · 4.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/**
* Wordcount application to read in a string from the command line or from a file and output the
* total words, total unique words and a count of each word's occurrence.
*
* @author Toby cook
* @version 1.0.0
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "words.h"
#include "readfile.h"
#include "tokenize.h"
#include "count.h"
#include "sort.h"
#define MAX_S 256
int main(int argc, char **argv)
{
char *word_data, *file_extension = "", *input_file_name = "", *output_file_name = "";
size_t length = 0, ignore_case = 0;
FILE *input_file, *output_file;
Dictionary *dictionary;
// Loop through arguments
for (int i = 0; i < argc; ++i)
{
// Checks to see if there is an input_file file name
if (strcmp(argv[i], "-i") == 0)
{
if (argv[i + 1] != NULL && strcmp(argv[i + 1], "-o"))
{
// Retrieve the file extension and compare
file_extension = argv[i + 1] + strlen(argv[i + 1]) - 4;
if (strcmp(file_extension, ".txt") == 0)
{
// Store file name
input_file_name = argv[i + 1];
}
else
{
// Set empty string
input_file_name = "";
printf("No input file specified.\n");
}
}
else
{
printf("No input file specified.\n");
}
}
// Checks to see if there is an output_file file name
if (strcmp(argv[i], "-o") == 0)
{
if (argv[i + 1] != NULL && strcmp(argv[i + 1], "-c"))
{
// Retrieve the file extension and compare
file_extension = argv[i + 1] + strlen(argv[i + 1]) - 4;
if (strcmp(file_extension, ".txt") == 0)
{
// Store file name
output_file_name = argv[i + 1];
}
else
{
// Set empty string
output_file_name = "";
printf("No output file specified.\n");
}
}
else
{
printf("No output file specified.\n");
}
}
// Checks if ignore case command has been entered
if (strcmp(argv[i], "-c") == 0)
{
ignore_case = 1;
}
}
input_file = fopen(input_file_name, "r");
output_file = fopen(output_file_name, "w");
// If there is an input file
if (input_file)
{
// Read contents of file into word buffer
read_file(input_file, &word_data, &length);
// Convert words to lower if user has entered -c command
if (ignore_case == 1)
strlwr(word_data);
}
else
{
// Declare temporary buffer to store fgets input_file
char buffer[MAX_S];
// Set input_file to read from stdin
input_file = stdin;
// Read input_file from the command line
printf("Please enter some text: ");
fgets(buffer, MAX_S, input_file);
// Assign words and length values
word_data = (ignore_case == 1) ? strlwr(buffer) : buffer;
length = strlen(word_data) + 1;
word_data[length-2] = '\0';
}
printf("\n");
// Get the total number of words
int word_count = count_words(word_data);
int total_words = word_count;
// Split words in buffer and store total word count
int unique_count = tokenize(&dictionary, word_data, &word_count);
// Sort words in descending order
descending_sort(&dictionary, unique_count);
if (ignore_case == 1)
printf("CHARACTER CASE IS BEING IGNORED!\n\n");
// Print to file if there is one specified
if (output_file)
{
fprintf(output_file, "The total words counted is: %d\n", total_words);
fprintf(output_file, "The total unique words counted is: %d\n\n", unique_count);
for (int i = 0; i < unique_count; ++i)
fprintf(output_file, "%s : %d\n", dictionary[i].word, dictionary[i].count);
fclose(output_file);
printf("Words have been printed to the file: %s\n", output_file_name);
}
else
{
printf("The total words counted is: %d\n", total_words);
printf("The total unique words counted is: %d\n\n", unique_count);
for (int i = 0; i < unique_count; ++i)
printf("%s : %d\n", dictionary[i].word, dictionary[i].count);
}
// Close the file and free the allocated memory for the structure
fclose(input_file);
for(int i = 0; i < unique_count; i++)
free(dictionary[i].word);
free(dictionary);
free(word_data);
return 0;
}