{"id":6112,"date":"2023-11-25T00:01:39","date_gmt":"2023-11-25T08:01:39","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=6112"},"modified":"2023-12-02T07:53:54","modified_gmt":"2023-12-02T15:53:54","slug":"finding-all-the-pangrams","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=6112","title":{"rendered":"Finding All the Pangrams"},"content":{"rendered":"<p>I introduced the pangram concept in <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6104\">last week&#8217;s Lesson<\/a>. The code demonstrated how to locate 7-letter words where no letter repeats, based on the online game <a href=\"https:\/\/www.nytimes.com\/puzzles\/spelling-bee\">Spelling Bee<\/a>. But a pangram need not be limited to just seven letters.<br \/>\n<!--more--><br \/>\nTo find pangrams in the digital dictionary file, I updated the code from last week&#8217;s Lesson. I decided to keep the minimum letter length to seven, but have no maximum limit. This change required a few modifications to the code.<\/p>\n<p>First, the <em>char_scan()<\/em> function is updated to include both the word to scan (<em>char<\/em> <code>*a<\/code>) and its length (<em>int<\/em> <code>len<\/code>). Further, I modified the function to check for an initial capital letter, hopefully to eliminate proper nouns. It also scans for apostrophes, which saves a lot of code in the <em>main()<\/em> function.<\/p>\n<p>The <em>main()<\/em> function is updated to account for the longer pangrams and output when a new, long pangram is found. Here is the code:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2023_11_25-Lesson.c\" rel=\"noopener\" target=\"_blank\">2023_11_25-Lesson.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#include &lt;ctype.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 char_scan(char *a,int len)\r\n{\r\n    int x,y;\r\n\r\n    <span class=\"comments\">\/* eliminate proper nouns *\/<\/span>\r\n    if( isupper(a[0]) )\r\n        return(FALSE);\r\n\r\n    <span class=\"comments\">\/* scan the characters *\/<\/span>\r\n    for( x=0; x&lt;len-2; x++ )\r\n    {\r\n        for( y=x+1; y&lt;len-1; y++ )\r\n        {\r\n            if( a[x] == a[y] )\r\n            {\r\n                <span class=\"comments\">\/* not unique *\/<\/span>\r\n                return(FALSE);\r\n            }\r\n            if( a[x] == '\\'' )\r\n                return(FALSE);\r\n        }\r\n    }\r\n    return(TRUE);\r\n}\r\n\r\nint main()\r\n{\r\n    FILE *dict;\r\n    int length,count,max;\r\n    char word[SIZE],*r;\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 pangrams *\/<\/span>\r\n    count = max = 0;\r\n    while( !feof(dict) )\r\n    {\r\n        memset(word,'\\0',SIZE);        <span class=\"comments\">\/* clear buffer *\/<\/span>\r\n        r = fgets(word,SIZE,dict);    <span class=\"comments\">\/* read a word *\/<\/span>\r\n        if( r==NULL )\r\n            break;\r\n        length = strlen(word);\r\n        if( length&gt;7 )\r\n        {\r\n            if( char_scan(word,length) )\r\n            {\r\n                if( length &gt; max )\r\n                {\r\n                    printf(\"%s\",word);\r\n                    max = length;\r\n                }\r\n                count++;\r\n            }\r\n        }\r\n    }\r\n    printf(\"I found %d pangrams!\\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 the <em>main()<\/em> function, <em>int<\/em> variable <code>length<\/code> stores the dictionary word&#8217;s letter count. If it&#8217;s greater than seven, <code>if( length&gt;7 )<\/code>, the <em>char_scan()<\/em> function is called. When this function returns TRUE, the value of <code>length<\/code> is compared with <em>int<\/em> variable <code>max<\/code> to check for long pangrams. The long pangram word is output. Variable <code>count<\/code> increments for all discovered pangrams, <code>count++<\/code>.<\/p>\n<p>Here&#8217;s a sample run:<\/p>\n<p><code>abdomen<br \/>\nabdomens<br \/>\nabducting<br \/>\nabductions<br \/>\nabridgments<br \/>\nambidextrous<br \/>\nambidextrously<br \/>\nI found 7089 pangrams!<\/code><\/p>\n<p>At least on my computer&#8217;s dictionary, the longest available pangram is <code>ambidextrously<\/code> (14 letters).<\/p>\n<p>In the Spelling Bee game, a pangram is limited to only those letters supplied with one letter required. Further, the letters can repeat. In <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6131\">next week&#8217;s Lesson<\/a>, I further update the code to better emulate the Spelling Bee puzzle.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>How many words in the digital dictionary are composed of unique letters? <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6112\">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-6112","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\/6112","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=6112"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6112\/revisions"}],"predecessor-version":[{"id":6148,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6112\/revisions\/6148"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6112"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6112"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6112"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}