diff options
Diffstat (limited to 'ex1-8.c')
-rw-r--r-- | ex1-8.c | 49 |
1 files changed, 37 insertions, 12 deletions
@@ -1,18 +1,43 @@ #include <stdio.h> #include <stdint.h> -int main(void) { - int c; - uint32_t nl = 0, tb = 0, bl = 0; - while ((c = getchar()) != EOF) { - if (c == ' ') { - bl++; - } else if (c == '\n') { - nl++; - } else if (c == '\t') { - tb++; - } +struct res { + uint64_t nl; + uint64_t tb; + uint64_t bl; +}; + +struct res nr(void) { + struct res r = { + .nl = 0, + .tb = 0, + .bl = 0. + }; + return r; +} + +static struct res _loop(struct res res) { + int c = getchar(); + if (c == EOF) { + return res; } - printf("nl=%ld tb=%ld bl=%ld\n", nl, tb, bl); + if (c == ' ') { + res = (struct res){ + .bl = res.bl+1, + .nl = res.nl, + .tb = res.tb, + }; + } else if (c == '\n') { + res.nl++; + } else if (c == '\t') { + res.tb++; + } + __attribute__((musttail)) + return _loop(res); +} + +int main(void) { + struct res r = _loop(nr()); + printf("nl=%ld tb=%ld bl=%ld\n", r.nl, r.tb, r.bl); return 0; } |