1

I am browsing for two days now and have not find a solution which meets to my solution. I am trying to write a Pangram function. I have tried many ways but could not succeed. My function IsPangram always give it is not Pangram suggest me some ways.

void isPangram()
{
    int i;
    int count = 0;
    char str[26];
    cout << "Enter a string to check if its Pangram or not: ";
    for (i = 0; i < 26; i++) {
        cin >> str[i];

        if (i >= 97 && i <= 122) {

            cout << "It is Pangram" << endl;
            break;
        } else {
            cout << "it is not Pangram" << endl;
            break;
        }

    }
}

int main()
{
    isPangram();
    system("pause");
}
Michael Petch
  • 46,082
  • 8
  • 107
  • 198
Qasim Ali
  • 47
  • 1
  • 7

4 Answers4

0

There are 3 issues in your code -

(i) You are trying to check if each character is a pangram, which is wrong.

(ii) For checking, you are checking against the index i instead of the character read which is str[i].

(iii) This statement if ( i >= 97 && i <= 122 ) will always evaluate to false since the value of i can be only between 0 and 26. Hence you are always getting not a pangram.

Try this -

void isPangram() {
    int i;
    int count = 0;
    char str[27];
    cout << "Enter a string to check if its Pangram or not: ";

    // Read the string
    cin >> str;

    short flagArr[26]; // Array to flag the characters used
    memset(flagArr, 0, sizeof(flagArr));
    bool panGramFlag = true;

    // Loop through the string and mark the characters read
    for (int i = 0; i < 27; i++) {
        if (str[i] >= 'A' && str[i] <= 'Z') {
            flagArr[str[i]-'A'] = 1;
        }
    }

    // Loop through flag array and check if any character is not used.
    for (int i = 0; i < 26; i++) {
        if (flagArr[i] == 0) {
           panGramFlag = false;
           cout << "It is not Pangram" << endl;
           break;             
        }
    }   

    if (panGramFlag)
        cout << "it is Pangram" << endl;

}

int main() {

    isPangram();

    system("pause");
}  
buchipper
  • 606
  • 4
  • 16
  • `flagArr[str[i]-'A'] = 1;` i could not understand this statement ? could you please explain it a little? @buchipper – Qasim Ali Nov 12 '15 at 07:56
  • @QasimAli Maybe you should start with a simpler program first and learn some basic C++. – Lukáš Bednařík Nov 12 '15 at 08:31
  • @QasimAli - `str[i]` is the character that was entered by the user. The character `'A'` has the value of 65. If the character at `str[i]` is 'B', then `flagArr[str[i]-'A']` translates to `flagArr['B'-'A']` which is `flagArr[66-65]` which is `flagArr[1]`. Thus if `str[i]` is 'B', then `flagArr[1]` will be set to 1 - which means the sentence contains the character 'B'. Hope this answers your question. – buchipper Nov 12 '15 at 09:11
  • This only works for upper-case alphabetic characters, which is weird. You could use `toupper`. Also, your program only has room to input a 26-byte string, so it can't read "the quick brown fox jumps over the lazy dog" or other standard examples. Your analysis of the bugs in the question look right, though. (Your `flagArr` only needs to be `bool` or `char`, no point in using `short`. And the input/output should be in `main`, passing a string to `isPangram`, otherwise not much point in making it a function. You have an `int i;` at the top which is shadowed by the `for( int i ...` vars.) – Peter Cordes Jun 06 '23 at 18:33
  • Also, normally you'd loop while `str[i] != 0`, instead of always scanning all the bytes in your input buffer regardless of the length of the input string. – Peter Cordes Jun 06 '23 at 18:42
0
#include<iostream>
using namespace std;

bool chkpangram(string &str)
{
    int m[26] = {0}, i, index;
    for(i = 0; i < str.length(); i++)
    {
        if (str[i] >= 65 && str[i] <= 90)
            index = str[i] - 65;
        else if(str[i] >= 97 && str[i] <= 122)
            index = str[i] - 97;

        m[index] = m[index]++;
    }
    for (int i=0; i<=25; i++)
        if (m[i] == 0)
            return (false);

    return true;
}

int main()
{
    string str = "The quick brown fox jumps over the lazy dog";
    if(str.length() < 26)
        cout<<"Not a Pangram";
    else
    {
        if(chkpangram(str) == true)
            cout<<"Is a Pangram";
        else
            cout<<"Not a Pangram";
    }
    return 0;
}
  • 1
    As this can be an answer to the OP question, try to provide more context rather than just paste some code. Please refer to [How do I write a good answer?](https://stackoverflow.com/help/how-to-answer) try to explain what was wrong and why, so other users with the same problem can understand your answer – rmjoia Oct 24 '17 at 06:24
  • Code only answers are discouraged. Always try to explain with your own words what your answer is about! – GhostCat Oct 24 '17 at 06:36
  • `m[index] = m[index]++;` looks like a bug, perhaps undefined behaviour in some revisions of C++. You post-increment but then assign the unincremented value to the array. Since you're checking for a pangram, `m[index] = 1;`, you don't need a count just a boolean. – Peter Cordes Jun 06 '23 at 02:48
-1

This is a simple one, think logically

#include<iostream.h>
#include<string.h>
#include<conio.h>
void isPangram()
{
    int i;
    char str[26];
    cout << "Enter a string to check if its Pangram or not: ";
    for (i = 0; i < 26; i++) {
    cin >> str[i];

    if ((str[i] >= 97 && str[i] <= 122)||((str[i] >= 65 && str[i] <= 91))
    {

        cout << "It is Pangram" << endl;
        break;
    } else {
        cout << "it is not Pangram" << endl;
        break;
    }

    }
}

int main()
{
    isPangram();
    getch();
    return 0;
}
Rich Benner
  • 7,873
  • 9
  • 33
  • 39
-1

//Optimal code using hashing

string str;
int i,h1[26]={0},flag=0;

for(i=0;str[i]!='\0';i++)
{
    h1[str[i]-'a']++;
}
for(i=0;i<26;i++)
{
    if(h1[i]!=1){
        flag=1;
        break;
    }
}
flag==1?cout<<"No"<<endl:cout<<"Yes"<<endl;

} //Hope it helps

  • `str` is a `char`, so how/why are you indexing it with `str[i]`? Did you mean it to be a `std::string`? – cigien May 04 '20 at 20:04
  • Sry for that..use string – Karan Dasari May 05 '20 at 08:50
  • This accesses outside the `h1` array if any input character isn't a lower-case ASCII letter. Also, that ternary at the bottom is horrible. `std::cout << flag ? "No\n" : "Yes\n"; Or just write a function that returns a bool. Oh, also, this is checking for each alphabetic character appearing exactly one (h1[i] == 1), not for a pangram which only needs a bool array. – Peter Cordes Jun 06 '23 at 02:35