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 <stdio.h>
<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[26] = { 0 };
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 >= 'a' && c <= 'z')
if (is_letter(c))
++max_count[c - 'a'];
++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[26] = { 0 };
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 - 'a'] > 0)
if (p >= word + min_length && count[index(central)] > 0)
printf("%s", word);
printf("%s", word);
} else if (c >= 'a' && c <= 'z') {
} else if (is_letter(c)) {
int i = c - 'a';
int i = index(c);
if (++count[i] > max_count[i]) {
if (++count[i] > max_count[i]) {
break;
break;