C++ program to check whether a String is a Pangram or not

Last Updated : 22 Aug, 2026

A pangram is a string that contains every letter of the English alphabet at least once, regardless of the order or frequency of the letters.

  • The check is case-insensitive, so uppercase and lowercase letters are treated as the same.
  • Non-alphabetic characters such as spaces, digits, and punctuation are ignored.

Examples:

Input: We promptly judged antique ivory buckles for the next prize
Output: Yes

Input: We promptly judged antique ivory buckles for the prize
Output: No

The first string contains all 26 letters of the English alphabet, while the second string is missing at least one letter.

Approaches to Check Whether a String is a Pangram

The following approaches can be used to check whether a string contains all 26 English alphabet letters:

1. Using a Boolean Array

This approach uses a Boolean array of size 26 to keep track of the letters present in the string.

Steps:

  1. Create a Boolean array of size 26 initialized to false.
  2. Traverse the string character by character.
  3. Convert uppercase letters to their corresponding lowercase index.
  4. Mark the corresponding position as true.
  5. Ignore spaces and other non-alphabetic characters.
  6. Check whether all 26 positions are marked true.
C++
#include <iostream>
#include <string>
using namespace std;

bool isPangram(const string& str)
{
    bool present[26] = {false};

    for (char ch : str) {
        if (ch >= 'A' && ch <= 'Z')
            ch = ch - 'A' + 'a';

        if (ch >= 'a' && ch <= 'z')
            present[ch - 'a'] = true;
    }

    for (bool found : present) {
        if (!found)
            return false;
    }

    return true;
}

int main()
{
    string str =
        "We promptly judged antique ivory "
        "buckles for the next prize";

    cout << (isPangram(str) ? "Yes" : "No");

    return 0;
}

Output
Yes

Explanation: The Boolean array stores whether each letter from a to z has appeared. For every alphabetic character, its index is calculated using ch - 'a', and that position is marked as true. If all 26 positions are marked, the string is a pangram.

2. Using STL set

This approach uses an STL set to store the distinct alphabetic characters present in the string.

Steps:

  1. Create an empty set of characters.
  2. Traverse the given string character by character.
  3. Convert each character to lowercase so that uppercase and lowercase letters are treated equally.
  4. Insert the character into the set if it is an English alphabet letter.
  5. Since a set stores only unique characters, duplicate letters are automatically ignored.
  6. After traversal, check whether the set contains all 26 English alphabet letters.
  7. f the set size is 26, the string is a pangram; otherwise, it is not.
C++
#include <bits/stdc++.h>
using namespace std;

string isPangram(string s) {

    // Convert each letter to lowercase
    transform(s.begin(), s.end(), s.begin(), ::tolower);

    // Create a frequency map of characters
    unordered_map<char, int> freq;
    for (char c : s) {
        if (isalpha(c)) {
            freq[c]++;
        }
    }

    // Check if the frequency map contains all 26 letters
    return freq.size() == 26 ? "Yes" : "No";
}

int main() {
    string str = "We promptly judged antique ivory buckles for the next prize";
    cout << isPangram(str) << endl;
    return 0;
}

Output
Yes

Explanation: The program converts each character to lowercase and inserts alphabetic characters into a set. Since a set stores only unique characters, its size represents the number of distinct letters found. If the size is 26, every English alphabet letter is present.

Comment