-2

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;
}
  • 2
    you print "pangram" already when you find a single letter. Anyhow your logic is a bit strange. Do you really want to make the user type the word letter by letter? Why do you use `alphabet[26]`? It looks more like `alphabet` is the user input, but not the 26 letters of the alphabet. What if the user wants to check a sentence that is 27 characters long? – 463035818_is_not_an_ai Mar 23 '17 at 11:08
  • ..btw you should include input, expected and actual output in the question – 463035818_is_not_an_ai Mar 23 '17 at 11:10
  • 1
    no offense, but this code has little to do with checking if a sentence is a pangram. I suggest you to step back, get a rubber duck and explain it what you want to do step by step – 463035818_is_not_an_ai Mar 23 '17 at 11:16
  • You should start again with the basic tutorials. About every part of your semantics looks wrong. There is no check if some specific letter is in the text, the for loop does always the same thing, just at another position of your variable (which is irrelevant here), it reads in the word within the loop instead of at the beginning... if I had to correct this program, I would delete the code and write new one. Don't want to offend you, but I think you lack some basic understanding of how algorithms work. – Aziuth Mar 23 '17 at 11:30
  • Other working algorithms in a C version of the same question: [Pangram in C using functions](https://stackoverflow.com/q/68200504) – Peter Cordes Jun 06 '23 at 02:22

1 Answers1

-1

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;
}
paper.plane
  • 1,201
  • 10
  • 17
  • The basic idea for the pangram algorithm is good here, but there are some implementation problems. You either need `alphabet[256]`, or you need to check for alphabetic as part of `(c|0x20)-'a'` as an index into the alphabet. Also, putting the prompting and I/O into the `pangram()` function kind of defeats the purpose of making it a separate function. `for ()` `if() {}` without braces for the `for()` is not recommended by most coding-style guides. Also, if you'd properly separated your functions it could just be `if() return false;` instead needing a bool variable and a `break`. – Peter Cordes Jun 06 '23 at 02:27
  • [Pangram in C using functions](https://stackoverflow.com/q/68200504) has this and other algorithms in C. (This code is already written in very much a C style, not using C++ `std::array` or `std::string`, or `std::find`. The memset could just be an array initializer. – Peter Cordes Jun 06 '23 at 02:30