Pangram checking in C++

Hello and welcome to this tutorial, folks!!!.

In this tutorial, we will discuss the approach for checking whether a string is a pangram or not in C++ programming. So let’s begin this tutorial…

  • So basically pangram is a type of sentence that contains all the 26 letters of the alphabets.
  • For solving this program we can make use of pre-build containers of the C++ STL. But we will try to keep the program very basic and therefore will only make use of “array”.
  • So before discussing the approach for this problem, I request you to first try yourself solving this problem using a pen and paper.

 

Approach for solving the problem

ProblemGiven a string check if it is Pangram or not.

Approach

  • Here we will be creating a function “chk_Pangram” which will check the string and return a boolean value depending upon the result. Also, we will make use of an array of size “26” used to store the frequency of all characters in the alphabets.
  • So if the frequency of any of the characters is zero i.e. the character is not present in the string we will return a “false” value otherwise “true” value. In other words, we can say that if all the alphabets occur at least one time in the string we will say that the string is a pangram.

(Note: Upper case and lower case letters are treated as the same characters. In other words, ‘a’ and ‘A’ will increment the frequency for ‘a’ character only.)

  • We will first iterate through the string and increment the index value number of the array by ‘1’ depending upon the character which is encountered.

For eg, if ‘c’ or ‘C’ is encountered in the string then we will increment the array value at index 2(Indexing starts from ‘0’) by 1.

  • Finally, we will iterate through the array of size 26 and check whether there is a ‘0’ value or not. If present then the function will return “false” or else “true”.

So now let’s try to write a code for the approach we have discussed above,

Program for Pangram Checking

// C++ program to determine whether the specified
// string is a Pangram or not.

#include <bits/stdc++.h>
using namespace std;

bool chk_Pangram(string s)
{
    // Int array of size 26 which is initially empty
    int Arr[26] = { 0 };

    // Iterating through the specified string
    for (int i = 0; i < s.length(); i++) 
    {
        // if i'th character in string is uppercase
        if (isupper(s[i])) 
        {
           Arr[ s[i] - 'A' ]++;
        }
        // else if i'th character in string is lowercase
        else if (islower(s[i])) 
        {
            Arr[ s[i] - 'a' ]++;
        }
     }
 
    // Iterating through array to check for the presence of 0 value
    for (int i = 0; i < 26; i++) 
    {
        if(Arr[i] == 0)
            return false;
    }
 return true;
}

//Main function
int main(void)
{
    string S = "The quick brown fox jumps over the lazy dog";

    if (chk_Pangram(S) == true)
        printf("%s is a Pangram", S.c_str());
    else
        printf("%s is not a Pangram", S.c_str());

 return 0;
}

Output for the above program is,

The quick brown fox jumps over the lazy dog is a Pangram

If we change the string to “The quick brown cat jumps over the lazy dog” then the output will be,

The quick brown cat jumps over the lazy dog is not a Pangram

Time Complexity:

  • As only a single loop will be used to iterate through the string to check for pangram, the time complexity would be O(n). Here ‘n’ is the length of the given string. The other loop that must be used to check through the updated array would be constant.

So that’s it for this tutorial. I hope you have understood the concept clearly.

Thanks for going through this tutorial.

Comment down for any queries.

Leave a Reply

Your email address will not be published.