Word wheel: Difference between revisions
Content added Content deleted
m (→{{header|Python}}: Expanded a docstring.) |
m (Tidied up C code a bit) |
||
Line 39: | Line 39: | ||
=={{header|C}}== |
=={{header|C}}== |
||
<lang c>#include < |
<lang c>#include <stdbool.h> |
||
#include <stdio.h> |
|||
#define MAX_WORD 80 |
#define MAX_WORD 80 |
||
#define LETTERS 26 |
|||
inline bool is_letter(char c) { return c >= 'a' && c <= 'z'; } |
|||
inline int index(char c) { return c - 'a'; } |
|||
void word_wheel(const char* letters, char central, int min_length, FILE* dict) { |
void word_wheel(const char* letters, char central, int min_length, FILE* dict) { |
||
int max_count[ |
int max_count[LETTERS] = { 0 }; |
||
for (const char* p = letters; *p; ++p) { |
for (const char* p = letters; *p; ++p) { |
||
char c = *p; |
char c = *p; |
||
if (c |
if (is_letter(c)) |
||
++max_count[c |
++max_count[index(c)]; |
||
} |
} |
||
char word[MAX_WORD + 1] = { 0 }; |
char word[MAX_WORD + 1] = { 0 }; |
||
while (fgets(word, MAX_WORD, dict)) { |
while (fgets(word, MAX_WORD, dict)) { |
||
int count[ |
int count[LETTERS] = { 0 }; |
||
for (const char* p = word; *p; ++p) { |
for (const char* p = word; *p; ++p) { |
||
char c = *p; |
char c = *p; |
||
if (c == '\n') { |
if (c == '\n') { |
||
if (p >= word + min_length && count[central |
if (p >= word + min_length && count[index(central)] > 0) |
||
printf("%s", word); |
printf("%s", word); |
||
} else if (c |
} else if (is_letter(c)) { |
||
int i = c |
int i = index(c); |
||
if (++count[i] > max_count[i]) { |
if (++count[i] > max_count[i]) { |
||
break; |
break; |