diff options
Diffstat (limited to 'src/word.c')
| -rw-r--r-- | src/word.c | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/src/word.c b/src/word.c new file mode 100644 index 0000000..f10a648 --- /dev/null +++ b/src/word.c @@ -0,0 +1,29 @@ +#include "word.h" + +static int read_char(FILE *fp); + +int read_word(FILE *fp, char *word, size_t length) { + int ch; + size_t position = 0; + + while ((ch = read_char(fp)) == ' ') + ; + + while (ch != ' ' && ch != EOF) { + if (position < length) { + word[position++] = ch; + } + ch = read_char(fp); + } + word[position] = '\0'; + return ch; +} + +static inline int read_char(FILE *fp) { + int ch = getc(fp); + if (ch == '\n' || ch == '\t') { + return ' '; + } + return ch; +} + |