summaryrefslogtreecommitdiff
path: root/ex1-8.c
diff options
context:
space:
mode:
Diffstat (limited to 'ex1-8.c')
-rw-r--r--ex1-8.c49
1 files changed, 37 insertions, 12 deletions
diff --git a/ex1-8.c b/ex1-8.c
index cc70b64..7e8c3bd 100644
--- a/ex1-8.c
+++ b/ex1-8.c
@@ -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;
}