0

I need to create a 2D string array and input to hi, up to 10 words, than to check if those words are pangram or not.
The program needs to stop receiving words if the words are pangram.
for example:

the
five
boxing
wizards
jump
quickly
It's a pangram?
Yes

but instead of stopping it's just keeps asking for words until it gets to 10. Also says that non-pangram sentences are pangram.

    #include<stdio.h>
    #include <string.h>

     #define ROWS 10
     #define COL 50
     #define NUM_OF_LETTERS 26


     int main()
     {
        char words[ROWS][COL] = {0};
        char used []['z' - 'a' + 1] = {0};
        int i = 0;
        int j=0;
        int count = 0;

        printf("Enter up to 10 words try to make a pangram\n");
        while(i<ROW&& count < NUM_OF_LETTERS)
        {
            fgets(words[i], ROW, stdin);
            words[i][strcspn(words[i], "\n")] = 0;
            int len = strlen(words[i]);
        
            for(j=0;j<COL;j++)
            {
                if(strcmp(words[j] ,used[j]) == 0)
                {
                    count++;
                }
            }
            i++;
        }   
        printf("It's a pangram?\n");
        if (count >= NUM_OF_LETTERS)
        {
            printf("Yes!\n");
        }
        else
        {
            printf("No\n");
        }
        return 0;
    }

And I can't use pointers.

जलजनक
  • 3,072
  • 2
  • 24
  • 30
  • 1
    For future reference, we know it's not working; it would be odd if it were functioning normally and you still posted it here. Rather, you should include, [in your question](https://stackoverflow.com/posts/70822135/edit), what it *is* doing, and how that differs from your expectations (which should also be included in your question), and any effort you've taken thus far to debug your program thus-far. Straight away I see a huge problem. `words[][COL] = {0}` declares a 1xCOL array of arrays, so as soon as you index past `words[0]` you're invoking undefined behavior (e.g. i >= 1 is UB). – WhozCraig Jan 23 '22 at 12:47
  • Your check for whether the input is a program needs to be inside the input loop, not after it. Or it needs to be in both places in case you do fill the word list. So you need to use functions to avoid code replication. – Jonathan Leffler Jan 23 '22 at 16:10
  • I tried what you said but it just counted the letters, it needs to check if all the characters of the alphabets is in the string. And that is what I cant do –  Jan 23 '22 at 16:54

1 Answers1

1
  1. Pangram :
    A pangram or holoalphabetic sentence is a sentence using every letter of a given alphabet at least once. Pangram - wiki

2. Count only unique appearance of letters in the input words. Words can have both upper & lower case letters.

#include <stdio.h>
#include <string.h>
#include <ctype.h>

#define MAX_INPUT_WORDS 10
#define MAX_WORD_SIZE   50
#define UNIQUE_LETTERS  26

int main()
{
    char* uniq_chars = "abcdefghijklmnopqrstuvwxyz";
    //create a look-up table for characters in an alphabet set
    char alpha_lt[256] = {0};
    for (int ai = 0; '\0' != uniq_chars[ai]; ++ai)
        alpha_lt[ (unsigned) uniq_chars[ai]] = 1;

    char words [MAX_INPUT_WORDS][MAX_WORD_SIZE];

    printf ("\nEnter up to 10 words, try to make a Pangram:\n");
    int uniq_count = 0; // tracks count of unique characters so far
    int wcount = 0;
    for (int wi = 0 ; wi < MAX_INPUT_WORDS; ++wi) {
        while (1 != scanf ("%49s", words[wi]));
        ++wcount;
        //count the unique characters from alphabet-set
        for (int ci = 0; '\0' != words[wi][ci]; ++ci) {
            //Pangram can have letter from different cases.
            int ichar = tolower (words[wi][ci]); // to homogenise upper/lower cases
            if (alpha_lt[ichar]) {    // uniq character not yet counted
                ++uniq_count;
                alpha_lt[ichar] = 0; // remove from LT; to skip
                // counting during next occurance
            }
        }
        if (UNIQUE_LETTERS == uniq_count)
            break;
    }

    printf ("\nIs it a Pangram?\n");
    printf ((UNIQUE_LETTERS == uniq_count) ? "Yes!\n" : "No\n");
    for (int wi = 0; wi < wcount;)
        printf ("%s ", words[wi++]);
    printf ("\n");

    return 0;
}
  1. Sample Pangrams for standard English:
"Waltz, bad nymph, for quick jigs vex." (28 letters)  
"Glib jocks quiz nymph to vex dwarf." (28 letters)  
"Sphinx of black quartz, judge my vow." (29 letters)  
"How vexingly quick daft zebras jump!" (30 letters)  
"The five boxing wizards jump quickly." (31 letters)  
"Jackdaws love my big sphinx of quartz." (31 letters)  
"Pack my box with five dozen liquor jugs." (32 letters)  
"The quick brown fox jumps over a lazy dog" (33 letters)  

Invalid Pangrams for testing:

ABCD EFGH IJK LMN OPQR STUVWXY Z  
abcdef g h i jklmnopqrstuvwxyz
जलजनक
  • 3,072
  • 2
  • 24
  • 30