C++ 编程语法

C++ Pangram Program

我编写了一个 C++ 程序来检查一个句子中是否包含所有字母。但是每次我 运行 它都显示它是一个 pangram。你能帮我看看问题出在哪里吗?

#include <iostream>
using namespace std;

void pangram() // The function to find if pangram or not.
{
    int c;
    char alphabet[26]; // 26 is all the letters in the alphabet
    for (c = 0; c < 26; c++) { // Checks all the letters
    cin >> alphabet[c];
    if ((alphabet[c] >= 97 && alphabet[c] <= 122) || ((alphabet[c] >= 65 && alphabet[c] <= 91))) { // alphabet[i] compares to range of characters a - z in ASCII TABLE
        cout << "pangram" << endl;
    break; // If its pangram, the program stops here.
    }
    else { // It continues if it's not pangram.
        cout << "Not pangram" << endl;
        break;
}
}
}


int main() // Main body just to call the pangram function.
{
    pangram();
    return 0;
}

您可能希望将您的代码重组为类似于下面的内容

#include <iostream>
#include <cstring>
using namespace std;

void pangram() // The function to find if pangram or not.
{
    int c;
    char alphabet[26]; // 26 is all the letters in the alphabet
    char sentence[1000];

    memset(alphabet, 0, 26);

    std::cout << "Enter sentence : ";
    std::cin.getline (sentence,1000);

    int len = strlen(sentence);

    for(int i=0; i<len; i++)
    {
        /* get the character from sentence[i] */
        /* update alphabet[character] = 1; */
    }

    bool flag = false;
    for(int i=0; i<26; i++)
        if(alphabet[i] == 0)
        {
            flag = true;
            break;
        }

    if(flag == false)
       cout << "Given sentence has all the characters.";
}


int main() // Main body just to call the pangram function.
{
    pangram();
    return 0;
}