Word wheel: Difference between revisions

Content added Content deleted
m (→‎{{header|Python}}: Expanded a docstring.)
m (Tidied up C code a bit)
Line 39:
 
=={{header|C}}==
<lang c>#include <stdiostdbool.h>
#include <stdio.h>
 
#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) {
int max_count[26LETTERS] = { 0 };
for (const char* p = letters; *p; ++p) {
char c = *p;
if (is_letter(c >= 'a' && c <= 'z'))
++max_count[index(c - 'a')];
}
char word[MAX_WORD + 1] = { 0 };
while (fgets(word, MAX_WORD, dict)) {
int count[26LETTERS] = { 0 };
for (const char* p = word; *p; ++p) {
char c = *p;
if (c == '\n') {
if (p >= word + min_length && count[index(central - 'a')] > 0)
printf("%s", word);
} else if (is_letter(c >= 'a' && c <= 'z')) {
int i = index(c - 'a');
if (++count[i] > max_count[i]) {
break;