{"id":6666,"date":"2024-11-16T00:01:37","date_gmt":"2024-11-16T08:01:37","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=6666"},"modified":"2024-11-23T09:02:30","modified_gmt":"2024-11-23T17:02:30","slug":"the-hexword-tally-and-total","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=6666","title":{"rendered":"The HexWord Tally and Total"},"content":{"rendered":"<p>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&#8217;s digital dictionary, which was demonstrated in <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6653\">last week&#8217;s Lesson<\/a>. Time to update the code!<br \/>\n<!--more--><br \/>\nI have three tasks to accomplish for this week&#8217;s update:<\/p>\n<ol>\n<li>Limit the results to words four letters or longer<\/li>\n<li>Display a count of the results<\/li>\n<li>Output each hexword&#8217;s decimal value<\/li>\n<\/ol>\n<p>To carry out the first task, the <em>strlen()<\/em> function is added to the code. If the function&#8217;s result is greater than three, the word is four letters long or longer:<\/p>\n<p><code>if( strlen(hexword) > 3 )<\/code><\/p>\n<p>This test takes place after the word is confirmed to be a hexword. If true, the word is output.<\/p>\n<p>The second task is to display a tally. For this job, I use my favorite <em>int<\/em> variable <code>count<\/code>, which must be declared:<\/p>\n<p><code>int count;<\/code><\/p>\n<p>It&#8217;s initialized before the main <em>while<\/em> 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&#8217;s Lesson:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2024_11_16-Lesson-a.c\" rel=\"noopener\" target=\"_blank\">2024_11_16-Lesson-a.c<\/a><\/h3>\n<pre class=\"screen\">\r\n<span class=\"comments\">\/* Hex Words *\/<\/span>\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\r\nint main()\r\n{\r\n    FILE *dict;\r\n    int count;\r\n    char word[SIZE],hexword[SIZE],*r,*w;\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\">\/* read the dictionary *\/<\/span>\r\n    count = 0;\r\n    while( !feof(dict) )\r\n    {\r\n        <span class=\"comments\">\/* read a word from the dictionary *\/<\/span>\r\n        r = fgets(word,SIZE,dict);\r\n        if( r==NULL )    <span class=\"comments\">\/* no word, done *\/<\/span>\r\n            break;\r\n\r\n        <span class=\"comments\">\/* remove newline *\/<\/span>\r\n        w = word;\r\n        while(*w)\r\n        {\r\n            if( *w=='\\n' )\r\n            {\r\n                *w = '\\0';\r\n                break;\r\n            }\r\n            w++;\r\n        }\r\n\r\n        <span class=\"comments\">\/* pull out only hex characters *\/<\/span>\r\n        sscanf(word,\"%[ABCDEFabcdef]\",hexword);\r\n\r\n        <span class=\"comments\">\/* compare hexword with original word *\/<\/span>\r\n        if( strcmp(word,hexword)==0 )\r\n        {\r\n            <span class=\"comments\">\/* choose words 4 letters long or longer *\/<\/span>\r\n            if( strlen(hexword) &gt; 3 )\r\n            {\r\n                printf(\"%3d: %-10s\\n\",count+1,hexword);\r\n                count++;\r\n            }\r\n        }\r\n    }\r\n\r\n\r\n    <span class=\"comments\">\/* clean-up *\/<\/span>\r\n    fclose(dict);\r\n    return(0);\r\n}<\/pre>\n<p>The update really starts at Line 49, which is the <em>strcmp()<\/em> comparison. If true, a hexword is found. This test is followed by <em>strlen()<\/em> to confirm that the word is at least four letters long. Then the word tally and word itself are output.<\/p>\n<p>Here&#8217;s a sample run (snipped):<\/p>\n<p><code>&nbsp;&nbsp;1:&nbsp;Bede<br \/>\n&nbsp;&nbsp;2:&nbsp;Beebe<br \/>\n&nbsp;&nbsp;3:&nbsp;DECed<br \/>\n&nbsp;&nbsp;4:&nbsp;Dacca<br \/>\n&nbsp;&nbsp;5:&nbsp;Dada<br \/>\n...<br \/>\n&nbsp;38:&nbsp;face<br \/>\n&nbsp;39:&nbsp;faced<br \/>\n&nbsp;40:&nbsp;fade<br \/>\n&nbsp;41:&nbsp;faded;<br \/>\n&nbsp;42:&nbsp;feed<\/code><\/p>\n<p>The program found 42 positive results in the dictionary, regular words which are also hexwords.<\/p>\n<p>The final task is to output the words&#8217; decimal values.  After all, the hexadecimal values are already present. Duh.<\/p>\n<p>To translate a string into a value, I use <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3662\">the <em>strtol()<\/em> function<\/a>, which translates value strings in various bases (not just base 10). This statement translates a hexadecimal value in variable <code>hexword<\/code> into a decimal value, stored in <em>long<\/em> variable <code>dec<\/code>:<\/p>\n<p><code>dec = strtol(hexword,NULL,16);<\/code><\/p>\n<p>The third argument, 16, informs the function to translate the string as a base 16 value. The result is then used in the <em>printf()<\/em> statement, which I&#8217;ve updated like this:<\/p>\n<p><code>printf(\"%3d: %-10s %10ld\\n\",<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;count+1,<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hexword,<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dec<br \/>\n);<\/code><\/p>\n<p><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2024_11_16-Lesson-b.c\" rel=\"noopener\" target=\"_blank\">Click here<\/a> to view the updated code on GitHub. Here is the updated output:<\/p>\n<p><code>&nbsp;&nbsp;1:&nbsp;Bede&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;48862<br \/>\n&nbsp;&nbsp;2:&nbsp;Beebe&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;782014<br \/>\n&nbsp;&nbsp;3:&nbsp;DECed&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;912621<br \/>\n&nbsp;&nbsp;4:&nbsp;Dacca&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;896202<br \/>\n&nbsp;&nbsp;5:&nbsp;Dada&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;56026<br \/>\n...<br \/>\n&nbsp;38:&nbsp;face&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;64206<br \/>\n&nbsp;39:&nbsp;faced&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1027309<br \/>\n&nbsp;40:&nbsp;fade&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;64222<br \/>\n&nbsp;41:&nbsp;faded&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1027565<br \/>\n&nbsp;42:&nbsp;feed&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;65261<\/code><\/p>\n<p>Of course, I&#8217;m not done with this program, though it&#8217;s correct and does what I want. No, I want more!<\/p>\n<p>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 <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6673\">next week&#8217;s Lesson<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Time to limit, tally, and calculate the total for &#8220;hexwords&#8221; in the digital dictionary. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6666\">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-6666","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\/6666","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=6666"}],"version-history":[{"count":6,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6666\/revisions"}],"predecessor-version":[{"id":6712,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6666\/revisions\/6712"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6666"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6666"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6666"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}