0

我需要创建一个二维字符串数组并输入 hi,最多 10 个单词,而不是检查这些单词是否是 pangram。
如果单词是 pangram,程序需要停止接收单词。
例如:

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

但它并没有停止,而是一直在询问单词,直到它达到 10。还说非 pangram 句子是 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;
    }

而且我不能使用指针。

4

1 回答 1

1
  1. Pangram
    pangram 或全字母句子是使用给定字母表的每个字母至少一次的句子。Pangram - 维基

2. 只计算输入单词中字母的唯一外观。单词可以有大写和小写字母。

#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. 标准英语的示例 Pangrams:
"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)  

用于测试的无效 Pangrams:

ABCD EFGH IJK LMN OPQR STUVWXY Z  
abcdef g h i jklmnopqrstuvwxyz
于 2022-03-05T16:11:39.373 回答