Saturday, December 2, 2023
HomeC ProgrammingTrying to find 7-Character Pangrams | C For Dummies Weblog

# Trying to find 7-Character Pangrams | C For Dummies Weblog

I’m a fan of the web sport Spelling Bee. On this sport, you employ a mix of seven letters to spell numerous phrases. Every phrase is not less than four-letters lengthy and should comprise a particular letter, proven within the heart of Determine 1. If you create a phrase that incorporates all seven letters, you’ve found a pangram.

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

The Spelling Bee puzzle proven in Determine 1 has 34 right solutions. Every one incorporates not less than 4 of the proven letters and all embrace the W, the middle tile. It’s a enjoyable sport that evokes me to surprise what number of phrases in English are pangrams?

In line with the Spelling Bee guidelines, the solutions can’t be correct nouns. They’ll often be international phrases, although this rule appears inconsistent. Regardless, I’m impressed. I cobbled collectively code from final week’s Lesson to search for 7-letter phrases. Then I added an replace to this month’s Train answer to search for distinctive characters in 7-letter phrases. Right here is the outcome:

### 2023_11_18-Lesson.c

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

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

int char_scan(char *a)
{
int measurement,x,y;

measurement = strlen(a) + 1;

for( x=0; x<size-2; x++ )
{
for( y=x+1; y<size-1; y++ )
{
if( a[x] == a[y] )
{

return(FALSE);
}
}
}
return(TRUE);
}

int essential()
{
FILE *dict;
int distinctive;
char phrase[SIZE],*r;

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

distinctive = 0;
whereas( !feof(dict) )
{
memset(phrase,'',SIZE);
r = fgets(phrase,SIZE,dict);
if( r==NULL )
break;
if( phrase[LENGTH-2]==''' )
proceed;
if( phrase[LENGTH]=='n' )
{
if( char_scan(phrase) )
{
printf("%s",phrase);
distinctive++;
}
}
}
printf("I discovered %d pangrams!n",distinctive);

fclose(dict);

return(0);
}```

The char_scan() perform is an replace to digit_scan() from this month’s Train answer. The snprintf() perform isn’t required as a string (`a`) is handed instantly. I retained the `measurement` variable, although all strings ought to be 7-characters lengthy. In any other case, the perform works the identical as from this month’s Train answer.

The essential() perform scans for `LENGTH` measurement phrases, the place `LENGTH` is 7 characters. For phrases of that measurement, the char_scan() perform known as and, when TRUE, the pangram phrase is output and a tally stored.

Once I ran this system with out checking for pangrams, it reported 11,945 7-letter phrases within the dictionary file. After including the char_scan() perform, the output confirmed 4,078 pangrams.

This system isn’t excellent. For instance, it flags the phrase vicuña as being 7-letters lengthy. It’s most likely deciphering the ñ as two-letters. Additional, the Spelling Bee sport tries to keep away from plurals, the place my program makes no such try.

All Spelling Bee puzzles have not less than one pangram answer. Generally the pangrams could be fairly lengthy. For subsequent week’s Lesson, I scan the digital dictionary to search for all doable pangram phrases seven letters or longer.

RELATED ARTICLES