added word count
This commit is contained in:
@@ -56,6 +56,38 @@ read_file_into_mem(const char* path)
|
||||
return buffer;
|
||||
}
|
||||
|
||||
static size_t
|
||||
word_count(const struct SPP_Stream s, const char* path)
|
||||
{
|
||||
size_t words = 0;
|
||||
bool in_word = false;
|
||||
|
||||
while (!spp_eof(s)) {
|
||||
char c = **s.cursor;
|
||||
|
||||
// Treat common ASCII whitespace as separators
|
||||
bool is_ws = (c == ' ' || c == '\t' || c == '\n' || c == '\r' ||
|
||||
c == '\v' || c == '\f');
|
||||
|
||||
if (is_ws) {
|
||||
in_word = false;
|
||||
(*s.cursor)++;
|
||||
} else {
|
||||
if (!in_word) {
|
||||
words++;
|
||||
printf("%s - Word Count: %lld\r", path, words);
|
||||
fflush(stdout);
|
||||
in_word = true;
|
||||
}
|
||||
(*s.cursor)++;
|
||||
}
|
||||
}
|
||||
|
||||
printf("%s - Word Count: %lld\n", path, words);
|
||||
fflush(stdout);
|
||||
return words;
|
||||
}
|
||||
|
||||
static size_t
|
||||
line_count(const struct SPP_Stream s, const char* path)
|
||||
{
|
||||
@@ -145,7 +177,7 @@ main(int argc, char* argv[])
|
||||
const char* buffer = read_file_into_mem(path);
|
||||
|
||||
struct SPP_Stream stream = { .cursor = &buffer };
|
||||
line_count(stream, path);
|
||||
word_count(stream, path);
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user