aboutsummaryrefslogtreecommitdiff
path: root/challenge-005/paulo-custodio/c/ch-2.c
blob: f9564af872017472dda4d1e50e571cb6638c9ee0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*
Challenge 005

Challenge #2
Write a program to find the sequence of characters that has the most anagrams.

create a hash of all words in dictionary where key is sorted list of letters
therefore two anagrams have the same key
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "uthash.h"

#define MAX_WORD    1024

struct anagram {
    char* key;
    int     count;
    UT_hash_handle hh;
};

struct anagram* anagrams = NULL;

void* check_mem(void* p) {
    if (!p) {
        fputs("Out of memory", stderr);
        exit(EXIT_FAILURE);
    }
    return p;
}

void trim(char* str) {
    int len = strlen(str);
    while (len > 0 && isspace(str[len - 1]))
        str[--len] = '\0';
}

void strtolower(char* str) {
    for (char* p = str; *p; p++)
        *p = tolower(*p);
}

int compare_char(const void* a, const void* b) {
    return *(const char*)a - *(const char*)b;
}

void word_key(char* key, const char* word) {
    strcpy(key, word);
    qsort(key, strlen(key), 1, compare_char);
}

int compare_anagrams(struct anagram* a, struct anagram* b) {
    return strcmp(a->key, b->key);
}

int main() {
    int max_anagrams = 0;
    char key[MAX_WORD], line[MAX_WORD];
    struct anagram* anagram, *tmp;

    FILE* fp = fopen("words.txt", "r");
    if (!fp) return EXIT_FAILURE;

    // collect anagram keys and count occurrences
    while (fgets(line, MAX_WORD, fp)) {
        trim(line);
        strtolower(line);
        word_key(key, line);

        HASH_FIND_STR(anagrams, key, anagram);
        if (!anagram) {
            anagram = check_mem(malloc(sizeof(struct anagram)));
            anagram->key = check_mem(strdup(key));
            anagram->count = 1;
            HASH_ADD_KEYPTR(hh, anagrams, anagram->key, strlen(anagram->key), anagram);
        }
        else
            anagram->count++;
        if (max_anagrams < anagram->count)
            max_anagrams = anagram->count;
    }
    fclose(fp);

    // list anagrams
    HASH_SRT(hh, anagrams, compare_anagrams);
    printf("Maximum of %d anagrams\n", max_anagrams);
    for (anagram = anagrams; anagram != NULL; anagram = anagram->hh.next) {
        if (anagram->count == max_anagrams)
            printf("%s\n", anagram->key);
    }

    // free memory
    HASH_ITER(hh, anagrams, anagram, tmp) {
        HASH_DEL(anagrams, anagram);
        free(anagram->key);
        free(anagram);
    }

    return EXIT_SUCCESS;
}