diff options
author | Marvin Borner | 2020-12-02 20:57:51 +0100 |
---|---|---|
committer | Marvin Borner | 2020-12-02 20:57:51 +0100 |
commit | fadf98eb91030a678a3750f62ed019a6a81e9969 (patch) | |
tree | b4055a36ba7be730b696371a741f2596e9ca00af | |
parent | 2215847cd4a8e686b78253b059775368e05fcaf2 (diff) |
I always forget the uselessness of strlen...
...in for loops obviously. strlen can be helpful sometimes
-rw-r--r-- | 2020/2/solve.c | 11 |
1 files changed, 5 insertions, 6 deletions
diff --git a/2020/2/solve.c b/2020/2/solve.c index afcf22f..6294771 100644 --- a/2020/2/solve.c +++ b/2020/2/solve.c @@ -1,7 +1,6 @@ #include <assert.h> #include <stdio.h> #include <stdlib.h> -#include <string.h> int part_one(FILE *fp) { @@ -12,13 +11,13 @@ int part_one(FILE *fp) while (getline(&line, &len, fp) != -1) { int low, high, cnt = 0; char ch; - char str[256]; + char str[42]; sscanf(line, "%d-%d %c: %s\n", &low, &high, &ch, &str[0]); - for (int i = 0; i < strlen(str); i++) { - if (str[i] == ch) { + + for (char *p = str; *p; p++) + if (*p == ch) cnt++; - } - } + if (cnt >= low && cnt <= high) res++; } |