Skip to content

Commit

Permalink
Refactor examples
Browse files Browse the repository at this point in the history
  • Loading branch information
dr8co committed Apr 14, 2024
1 parent 64e6967 commit 785ddca
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions examples/word_stats.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@ int main(const int argc, char *argv[]) {
}
// Read the file into a buffer.
char buffer[file_size + 1];
fread(buffer, sizeof(char), file_size, file);
if (fread(buffer, sizeof(char), file_size, file) != file_size) {
fputs("Failed to read the file.\n", stderr);
return 1;
}

buffer[file_size] = '\0';
fclose(file);

Expand All @@ -64,9 +68,7 @@ int main(const int argc, char *argv[]) {
// Count the number of words and characters in the file.
for (size_t i = 0; i < string_length(s); ++i) {
if (isspace(string_at(s, i))) {
if (in_word) {
in_word = false;
}
if (in_word) in_word = false;
} else {
if (!in_word) {
in_word = true;
Expand All @@ -76,9 +78,11 @@ int main(const int argc, char *argv[]) {
}
}

printf("Word count: %zu\n", word_count);
printf("Character count: %zu\n", char_count);
printf("Average word length: %.2f\n", (double) char_count / word_count);
if (word_count && char_count) {
printf("Word count: %zu\n", word_count);
printf("Character count: %zu\n", char_count);
printf("Average word length: %.2f\n", (double) char_count / word_count);
} else fputs("The file contains binary data.\n", stderr);

string_free(s);
return 0;
Expand Down

0 comments on commit 785ddca

Please sign in to comment.