diff options
| author | Martial Simon <msimon_fr@hotmail.com> | 2025-09-15 01:08:27 +0200 |
|---|---|---|
| committer | Martial Simon <msimon_fr@hotmail.com> | 2025-09-15 01:08:27 +0200 |
| commit | c9b6b9a5ca082fe7c1b6f58d7713f785a9eb6a5c (patch) | |
| tree | 3e4f42f93c7ae89a364e4d51fff6e5cec4e55fa9 /rushs/tinyprintf/io_count_words | |
add: graphs et rushs
Diffstat (limited to 'rushs/tinyprintf/io_count_words')
| -rw-r--r-- | rushs/tinyprintf/io_count_words/count_words.c | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/rushs/tinyprintf/io_count_words/count_words.c b/rushs/tinyprintf/io_count_words/count_words.c new file mode 100644 index 0000000..8b8c9a5 --- /dev/null +++ b/rushs/tinyprintf/io_count_words/count_words.c @@ -0,0 +1,33 @@ +#include <stdio.h> + +int count_words(const char *file_in) +{ + if (file_in == NULL) + { + return -1; + } + + FILE *f = fopen(file_in, "r"); + if (f == NULL) + { + return -1; + } + + int word = 0; + int count = 0; + int c; + while ((c = fgetc(f)) != EOF) + { + if ((c == ' ' || c == '\n' || c == '\t') && word == 1) + { + word = 0; + } + if (c != ' ' && c != '\n' && c != '\t' && word == 0) + { + word = 1; + count++; + } + } + + return count; +} |
