diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-04-24 12:56:02 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-04-24 12:56:02 +0100 |
| commit | b66d98af00713b84c1917037c67c623ca8207476 (patch) | |
| tree | 69201ffe2925386448a9ad367f327dc62856dd4c | |
| parent | 25c88efc3e83fc333e98da6a0157e1853be61044 (diff) | |
| parent | 2c137d165436338b6dbd5cb7c540a091fc3368a9 (diff) | |
| download | perlweeklychallenge-club-b66d98af00713b84c1917037c67c623ca8207476.tar.gz perlweeklychallenge-club-b66d98af00713b84c1917037c67c623ca8207476.tar.bz2 perlweeklychallenge-club-b66d98af00713b84c1917037c67c623ca8207476.zip | |
Merge pull request #9983 from zapwai/branch-for-prev
C update
| -rw-r--r-- | challenge-265/zapwai/c/ch-2.c | 39 |
1 files changed, 23 insertions, 16 deletions
diff --git a/challenge-265/zapwai/c/ch-2.c b/challenge-265/zapwai/c/ch-2.c index a1e74ec231..d8b4dd7f1f 100644 --- a/challenge-265/zapwai/c/ch-2.c +++ b/challenge-265/zapwai/c/ch-2.c @@ -3,23 +3,25 @@ #include <ctype.h> #include <string.h> #include <stdbool.h> +#define SETSIZE 100 +#define WORDSIZE 30 typedef struct { - char* key; + char key; int value; } item; -item* val(item* items, size_t size, char* key) { +item* get(item* items, int size, char key) { for (size_t i = 0; i < size; i++) - if (strcmp(items[i].key, key) == 0) + if (items[i].key == key) return &items[i]; return NULL; } -void insert(item* items, size_t size, char* key, int val) { +void insert(item* items, size_t size, char key, int val) { int cnt = 0; - for (size_t i = 0; i < size; i++) { - if (strcmp(items[i].key, key) == 0) { + for (int i = 0; i < size; i++) { + if (items[i].key == key) { items[i].value = val; break; } @@ -32,18 +34,18 @@ void insert(item* items, size_t size, char* key, int val) { } item* freq(char* stringy, int *size) { - item* f = malloc(100 * sizeof(item)); - size_t f_size = 0; + item* f = malloc(SETSIZE * sizeof(item)); + int f_size = 0; for (int i = 0; i < strlen(stringy); i++) { char letter = stringy[i]; if (isalpha(letter)) { char let = tolower(letter); - item* cell = val(f, f_size, &let); + item* cell = get(f, f_size, let); if (!cell) { - insert(f, f_size, &let, 1); + insert(f, f_size, let, 1); f_size++; } else { - insert(f, f_size, &let, cell->value + 1); + insert(f, f_size, let, cell->value + 1); } } } @@ -55,16 +57,20 @@ item* freq(char* stringy, int *size) { bool hash_contains(item* g, item* f, int g_size, int f_size) { int cnt = 0; for (int i = 0; i < f_size; i++) { - char* key = f[i].key; - item* gcell = val(g, g_size, key); + char key = f[i].key; + item* gcell = get(g, g_size, key); if (!gcell) { + free(f); + free(g); return false; } else { - item* fcell = val(f, f_size, key); + item* fcell = get(f, f_size, key); if (fcell->value <= gcell->value) cnt++; } } + free(f); + free(g); return (cnt == f_size); } @@ -77,7 +83,7 @@ void proc(char* stringy, char* strlist[], int slen) { int f_size = 0; int g_size = 0; if (hash_contains(freq(strlist[i], &g_size), freq(stringy, &f_size), g_size, f_size)) { - ans[len] = malloc(30 * sizeof(char)); + ans[len] = malloc(WORDSIZE * sizeof(char)); strcpy(ans[len], strlist[i]); len++; } @@ -85,7 +91,8 @@ void proc(char* stringy, char* strlist[], int slen) { } printf("\n"); int min = strlen(ans[0]); - char* answer = malloc(sizeof(char) * 30); + char* answer = malloc(sizeof(char) * WORDSIZE); + strcpy(answer, ans[0]); for (int i = 0; i < len; i++) { if (strlen(ans[i]) < min) { min = strlen(ans[i]); |
