How to resolve the algorithm Word wheel step by step in the C programming language

Published on 7 June 2024 03:52 AM
#C

How to resolve the algorithm Word wheel step by step in the C programming language

Table of Contents

Problem Statement

A "word wheel" is a type of word game commonly found on the "puzzle" page of newspapers. You are presented with nine letters arranged in a circle or 3×3 grid. The objective is to find as many words as you can using only the letters contained in the wheel or grid. Each word must contain the letter in the centre of the wheel or grid. Usually there will be a minimum word length of 3 or 4 characters. Each letter may only be used as many times as it appears in the wheel or grid.

Write a program to solve the above "word wheel" puzzle. Specifically:

A "word" is defined to be any string contained in the file located at   http://wiki.puzzlers.org/pub/wordlists/unixdict.txt. If you prefer to use a different dictionary,   please state which one you have used. Word wheel puzzles usually state that there is at least one nine-letter word to be found. Using the above dictionary, find the 3x3 grids with at least one nine-letter solution that generate the largest number of words of three or more letters.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Word wheel step by step in the C programming language

This C program finds all the words in a dictionary that can be formed using a given set of letters and that contain a specific central letter. It also ensures that the words have a minimum length.

  • The program reads the dictionary from a file and the input parameters from the command line.
  • It creates an array max_count that stores the maximum count of each letter in the given set of letters.
  • It iterates over the dictionary, reading each word one line at a time.
  • For each word, it creates an array count that stores the count of each letter in the word.
  • It iterates over the word, checking each letter.
  • If the letter is a newline character, it checks if the word has a minimum length and if it contains the central letter. If so, it prints the word.
  • If the letter is a letter, it increments its count in the count array. If the count exceeds the maximum count for that letter in the max_count array, it breaks out of the loop.
  • If the letter is not a letter or a newline character, it breaks out of the loop.

Source code in the c programming language

#include <stdbool.h>
#include <stdio.h>

#define MAX_WORD 80
#define LETTERS 26

bool is_letter(char c) { return c >= 'a' && c <= 'z'; }

int index(char c) { return c - 'a'; }

void word_wheel(const char* letters, char central, int min_length, FILE* dict) {
    int max_count[LETTERS] = { 0 };
    for (const char* p = letters; *p; ++p) {
        char c = *p;
        if (is_letter(c))
            ++max_count[index(c)];
    }
    char word[MAX_WORD + 1] = { 0 };
    while (fgets(word, MAX_WORD, dict)) {
        int count[LETTERS] = { 0 };
        for (const char* p = word; *p; ++p) {
            char c = *p;
            if (c == '\n') {
                if (p >= word + min_length && count[index(central)] > 0)
                    printf("%s", word);
            } else if (is_letter(c)) {
                int i = index(c);
                if (++count[i] > max_count[i]) {
                    break;
                }
            } else {
                break;
            }
        }
    }
}

int main(int argc, char** argv) {
    const char* dict = argc == 2 ? argv[1] : "unixdict.txt";
    FILE* in = fopen(dict, "r");
    if (in == NULL) {
        perror(dict);
        return 1;
    }
    word_wheel("ndeokgelw", 'k', 3, in);
    fclose(in);
    return 0;
}


  

You may also check:How to resolve the algorithm Integer comparison step by step in the C programming language
You may also check:How to resolve the algorithm Combinations step by step in the C programming language
You may also check:How to resolve the algorithm Terminal control/Dimensions step by step in the C programming language
You may also check:How to resolve the algorithm Caesar cipher step by step in the C programming language
You may also check:How to resolve the algorithm Ulam spiral (for primes) step by step in the C programming language