Exemplo n.º 1
0
/* helper for when closing up (recursively) */
void trie_dispose(TrieNode* t)
{
	int i;

	if (t) {
		for (i = 0; i < 26; i++)
			trie_dispose(t->edges[i]);
		free(t);
	}
	return;
}
Exemplo n.º 2
0
int main(int argc, char **argv)
{
    if (argc < 3) {
        printf("Usage: scramble dict_file board_file\n");
        return RESULT_INVALID_USAGE;
    }

    FILE *board_file = fopen(argv[2], "r");
    char line[LINE_LENGTH];
    int line_no = 0;

    while (line_no < BOARD_HEIGHT && fgets(line, BOARD_WIDTH + 2, board_file)) {
        for (size_t i = 0 ; i < BOARD_WIDTH ; ++i) {
            if (!isalpha(line[i])) {
                printf("all spaces must contain letters\n");
                return RESULT_INVALID_BOARD;
            }
            line[i] = tolower(line[i]);
        }
        strncpy(board[line_no++], line, BOARD_WIDTH);
        memset(line, 0, sizeof(line));
    }
    if (line_no < BOARD_HEIGHT) {
        printf("you must provide %i lines\n", BOARD_HEIGHT);
        return RESULT_INVALID_BOARD;
    }
    fclose(board_file);


    FILE *dict_file = fopen(argv[1], "r");
    dict_trie = trie_init();

    while (fgets(line, LINE_LENGTH, dict_file)) {
        /* remove trailing newlines before inserting into trie */
        char *pos;
        if ((pos = strchr(line, '\n'))) *pos = '\0';
        trie_insert(dict_trie, line);
    }
    fclose(dict_file);
    
    print_words();

    trie_dispose(dict_trie);

    return RESULT_SUCCESS;
}