The HexWord Tally and Total

The dictionary is full of words composed of only the letters A through F, which are also hexadecimal digits. These English language hexwords can be pulled from the computer’s digital dictionary, which was demonstrated in last week’s Lesson. Time to update the code!

I have three tasks to accomplish for this week’s update:

  1. Limit the results to words four letters or longer
  2. Display a count of the results
  3. Output each hexword’s decimal value

To carry out the first task, the strlen() function is added to the code. If the function’s result is greater than three, the word is four letters long or longer:

if( strlen(hexword) > 3 )

This test takes place after the word is confirmed to be a hexword. If true, the word is output.

The second task is to display a tally. For this job, I use my favorite int variable count, which must be declared:

int count;

It’s initialized before the main while loop. After a positive hexword match is found, this variable is updated. These additions are found near the end of this source code file, which updates the code from last week’s Lesson:

2024_11_16-Lesson-a.c

/* Hex Words */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* this code assumes the following path is valid */
#define DICTIONARY "/usr/share/dict/words"
#define SIZE 32

int main()
{
    FILE *dict;
    int count;
    char word[SIZE],hexword[SIZE],*r,*w;

    /* open the dictionary */
    dict = fopen(DICTIONARY,"r");
    if( dict==NULL )
    {
        fprintf(stderr,"Unable to open %s\n",DICTIONARY);
        exit(1);
    }

    /* read the dictionary */
    count = 0;
    while( !feof(dict) )
    {
        /* read a word from the dictionary */
        r = fgets(word,SIZE,dict);
        if( r==NULL )    /* no word, done */
            break;

        /* remove newline */
        w = word;
        while(*w)
        {
            if( *w=='\n' )
            {
                *w = '\0';
                break;
            }
            w++;
        }

        /* pull out only hex characters */
        sscanf(word,"%[ABCDEFabcdef]",hexword);

        /* compare hexword with original word */
        if( strcmp(word,hexword)==0 )
        {
            /* choose words 4 letters long or longer */
            if( strlen(hexword) > 3 )
            {
                printf("%3d: %-10s\n",count+1,hexword);
                count++;
            }
        }
    }


    /* clean-up */
    fclose(dict);
    return(0);
}

The update really starts at Line 49, which is the strcmp() comparison. If true, a hexword is found. This test is followed by strlen() to confirm that the word is at least four letters long. Then the word tally and word itself are output.

Here’s a sample run (snipped):

  1: Bede
  2: Beebe
  3: DECed
  4: Dacca
  5: Dada
...
 38: face
 39: faced
 40: fade
 41: faded;
 42: feed

The program found 42 positive results in the dictionary, regular words which are also hexwords.

The final task is to output the words’ decimal values. After all, the hexadecimal values are already present. Duh.

To translate a string into a value, I use the strtol() function, which translates value strings in various bases (not just base 10). This statement translates a hexadecimal value in variable hexword into a decimal value, stored in long variable dec:

dec = strtol(hexword,NULL,16);

The third argument, 16, informs the function to translate the string as a base 16 value. The result is then used in the printf() statement, which I’ve updated like this:

printf("%3d: %-10s %10ld\n",
        count+1,
        hexword,
        dec
);

Click here to view the updated code on GitHub. Here is the updated output:

  1: Bede            48862
  2: Beebe          782014
  3: DECed          912621
  4: Dacca          896202
  5: Dada            56026
...
 38: face            64206
 39: faced         1027309
 40: fade            64222
 41: faded         1027565
 42: feed            65261

Of course, I’m not done with this program, though it’s correct and does what I want. No, I want more!

Currently, the output is sorted alphabetically, which is how the digital dictionary is read. But I want to sort the list by value! I begin to tackle this task in next week’s Lesson.

Leave a Reply