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");
}
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");
}