diff options
Diffstat (limited to 'rushs/evalexpr/io_count_words')
| -rw-r--r-- | rushs/evalexpr/io_count_words/count_words.c | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/rushs/evalexpr/io_count_words/count_words.c b/rushs/evalexpr/io_count_words/count_words.c new file mode 100644 index 0000000..8b8c9a5 --- /dev/null +++ b/rushs/evalexpr/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; +} |
