{"id":6131,"date":"2023-12-02T00:01:21","date_gmt":"2023-12-02T08:01:21","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=6131"},"modified":"2023-11-25T11:59:44","modified_gmt":"2023-11-25T19:59:44","slug":"almost-a-spelling-bee-like-scan","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=6131","title":{"rendered":"Almost a Spelling Bee-Like Scan"},"content":{"rendered":"<p>Scanning the digital dictionary is fun, but it&#8217;s gone on way too long! I was going to end this series with code that solves the Spelling Bee game, but decided to end things with this Lesson. This final post scans the dictionary for words that match a given clutch of characters.<br \/>\n<!--more--><br \/>\n<a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6112\">Last week&#8217;s Lesson<\/a> focused on pangrams. For this week&#8217;s update, I sought to match just a collection of seven letters, those shown in Figure 1.<\/p>\n<div id=\"attachment_6105\" style=\"width: 298px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-6105\" src=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2023\/11\/1118-figure1-288x300.png\" alt=\"\" width=\"288\" height=\"300\" class=\"size-medium wp-image-6105\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2023\/11\/1118-figure1-288x300.png 288w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2023\/11\/1118-figure1.png 438w\" sizes=\"auto, (max-width: 288px) 100vw, 288px\" \/><p id=\"caption-attachment-6105\" class=\"wp-caption-text\">Figure 1. Seven tiles from the Spelling Bee game. One of the pangrams is WARPING, which uses all seven letters.<\/p><\/div>\n<p>The updated code reads though the dictionary, plucking out one word at a time. Whan a word is longer than four characters, a scan is made to match letters A, G, I, N, P, R, or W. The word must be composed only of these letters in any combination. Any other letter (or character) disqualifies the word. These letters are set in the declaration of variable <code>format_string[]<\/code> at the start of the code.<\/p>\n<p>The secret to weeding out specific letters is the <em>sscanf()<\/em> function. This function uses the format string specified to filter the dictionary word stored in variable <code>word[]<\/code>. If the dictionary word contains only the letters listed, the resulting word is saved in the <code>match[]<\/code> buffer. These two buffers are compared to thin out partial matches. When they fully match, the word is output and tallied.<\/p>\n<p>You can <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4341\">click here<\/a> to read more about how the <em>scanf()<\/em> function is used to filter words. It&#8217;s a seldom-used feature of this function, but one that works wonderfully for my Spelling Bee\/dictionary filter. In the code, I used the <em>sscanf()<\/em> version of the function, which scans a buffer and not standard input.<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2023_12_02-Lesson-a.c\" rel=\"noopener\" target=\"_blank\">2023_12_02-Lesson-a.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n#include &lt;string.h&gt;\r\n\r\n<span class=\"comments\">\/* this code assumes the following path is valid *\/<\/span>\r\n#define DICTIONARY \"\/usr\/share\/dict\/words\"\r\n#define SIZE 32\r\n#define TRUE 1\r\n#define FALSE 0\r\n\r\nint main()\r\n{\r\n    FILE *dict;\r\n    int length,count;\r\n    char word[SIZE],*r,match[SIZE];\r\n    const char format_string[] = \"%[aginprw]\";\r\n\r\n    <span class=\"comments\">\/* open the dictionary *\/<\/span>\r\n    dict = fopen(DICTIONARY,\"r\");\r\n    if( dict==NULL )\r\n    {\r\n        fprintf(stderr,\"Unable to open %s\\n\",DICTIONARY);\r\n        exit(1);\r\n    }\r\n\r\n    <span class=\"comments\">\/* scan for matching words *\/<\/span>\r\n    count = 0;\r\n    while( !feof(dict) )\r\n    {\r\n        memset(word,'\\0',SIZE);        <span class=\"comments\">\/* clear buffer *\/<\/span>\r\n        memset(match,'\\0',SIZE);\r\n        r = fgets(word,SIZE,dict);    <span class=\"comments\">\/* read a word *\/<\/span>\r\n        if( r==NULL )\r\n            break;\r\n\r\n        <span class=\"comments\">\/* remove newline *\/<\/span>\r\n        r = word;\r\n        while(*r)\r\n        {\r\n            if( *r=='\\n' )\r\n            {\r\n                *r = '\\0';\r\n                break;\r\n            }\r\n            r++;\r\n        }\r\n\r\n        <span class=\"comments\">\/* process the words *\/<\/span>\r\n        length = strlen(word);\r\n        <span class=\"comments\">\/* greater than four letters *\/<\/span>\r\n        if( length&gt;4 )\r\n        {\r\n            sscanf(word,format_string,match);\r\n            if( strcmp(word,match)==0 )\r\n            {\r\n                printf(\"%s\\n\",word);\r\n                count++;\r\n            }\r\n        }\r\n    }\r\n    printf(\"I found %d solutions!\\n\",count);\r\n\r\n    <span class=\"comments\">\/* close *\/<\/span>\r\n    fclose(dict);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>In this code, I added a routine to remove the newline from the word read from the dictionary. This step is necessary to match the word generated by the <em>sscanf()<\/em> function, which doesn&#8217;t append the newline.<\/p>\n<p>Here&#8217;s a sample run:<\/p>\n<p><code>again<br \/>\naging<br \/>\nagrarian<br \/>\nairing<br \/>\nangina<br \/>\naping<br \/>\n...<br \/>\nwiping<br \/>\nwiring<br \/>\nwrapping<br \/>\nwring<br \/>\nwringing<br \/>\nI found 82 solutions!<\/code><\/p>\n<p>Not all of these words match the Spelling Bee solutions. The code doesn&#8217;t check for the required character (W in this example). More code could be added. In fact, I&#8217;ll probably do so on my own, though it seems like an obsession.<\/p>\n<p>You can <a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2023_12_02-Lesson-b.c\" rel=\"noopener\" target=\"_blank\">click here<\/a> for a version of this code that allows you to input the string of characters to match. I really got carried away with it, doing all sorts of error-checking and nerfing. I may continue coding that version of the program to prompt for the key letter and identify pangrams. But for now, my dictionary-reading series has concluded.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here is one more stab at solving the Spelling Bee without turning this series into an obsession. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6131\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-6131","post","type-post","status-publish","format-standard","hentry","category-main"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6131","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6131"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6131\/revisions"}],"predecessor-version":[{"id":6141,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6131\/revisions\/6141"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6131"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6131"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6131"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}