Monday, May 6, 2024
HomeC ProgrammingVirtually a Spelling Bee-Like Scan

Virtually a Spelling Bee-Like Scan


Scanning the digital dictionary is enjoyable, however it’s gone on method too lengthy! I used to be going to finish this sequence with code that solves the Spelling Bee recreation, however determined to finish issues with this Lesson. This remaining submit scans the dictionary for phrases that match a given clutch of characters.

Final week’s Lesson targeted on pangrams. For this week’s replace, I sought to match only a assortment of seven letters, these proven in Determine 1.

Determine 1. Seven tiles from the Spelling Bee recreation. One of many pangrams is WARPING, which makes use of all seven letters.

The up to date code reads although the dictionary, plucking out one phrase at a time. Whan a phrase is longer than 4 characters, a scan is made to match letters A, G, I, N, P, R, or W. The phrase should be composed solely of those letters in any mixture. Another letter (or character) disqualifies the phrase. These letters are set within the declaration of variable format_string[] at the beginning of the code.

The key to removing particular letters is the sscanf() perform. This perform makes use of the format string specified to filter the dictionary phrase saved in variable phrase[]. If the dictionary phrase incorporates solely the letters listed, the ensuing phrase is saved within the match[] buffer. These two buffers are in comparison with skinny out partial matches. Once they absolutely match, the phrase is output and tallied.

You’ll be able to click on right here to learn extra about how the scanf() perform is used to filter phrases. It’s a seldom-used function of this perform, however one which works splendidly for my Spelling Bee/dictionary filter. Within the code, I used the sscanf() model of the perform, which scans a buffer and never commonplace enter.

2023_12_02-Lesson-a.c


#embrace <stdio.h>
#embrace <stdlib.h>
#embrace <string.h>


#outline DICTIONARY "/usr/share/dict/phrases"
#outline SIZE 32
#outline TRUE 1
#outline FALSE 0

int predominant()
{
    FILE *dict;
    int size,rely;
    char phrase[SIZE],*r,match[SIZE];
    const char format_string[] = "%[aginprw]";

    
    dict = fopen(DICTIONARY,"r");
    if( dict==NULL )
    {
        fprintf(stderr,"Unable to open %sn",DICTIONARY);
        exit(1);
    }

    
    rely = 0;
    whereas( !feof(dict) )
    {
        memset(phrase,'',SIZE);        
        memset(match,'',SIZE);
        r = fgets(phrase,SIZE,dict);    
        if( r==NULL )
            break;

        
        r = phrase;
        whereas(*r)
        {
            if( *r=='n' )
            {
                *r="";
                break;
            }
            r++;
        }

        
        size = strlen(phrase);
        
        if( size>4 )
        {
            sscanf(phrase,format_string,match);
            if( strcmp(phrase,match)==0 )
            {
                printf("%sn",phrase);
                rely++;
            }
        }
    }
    printf("I discovered %d options!n",rely);

    
    fclose(dict);

    return(0);
}

On this code, I added a routine to take away the newline from the phrase learn from the dictionary. This step is important to match the phrase generated by the sscanf() perform, which doesn’t append the newline.

Right here’s a pattern run:

once more
getting old
agrarian
airing
angina
aping
...
wiping
wiring
wrapping
wring
wringing
I discovered 82 options!

Not all of those phrases match the Spelling Bee options. The code doesn’t verify for the required character (W on this instance). Extra code could possibly be added. In reality, I’ll most likely accomplish that by myself, although it looks like an obsession.

You’ll be able to click on right here for a model of this code that lets you enter the string of characters to match. I actually acquired carried away with it, doing all types of error-checking and nerfing. I could proceed coding that model of this system to immediate for the important thing letter and establish pangrams. However for now, my dictionary-reading sequence has concluded.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments