Search code examples
c++pangram

C++ Pangram Program


I've made a c++ program to check if a sentence has all the alphabet letters in it. But every time I run it it shows that it is a pangram. Can you help me find where the problem is?

#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;
}

Solution

  • Your may want to restructure your code to something similar as below

    #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;
    }