Given string str, the task is to check whether a string is pangram or not using in C++.
A string is a Pangram if the string contains all the English alphabet letters.
Examples:
Input: str = “We promptly judged antique ivory buckles for the next prize”
Output: Yes
Explanations: In the above string, str has all the English alphabet letters.Input: str = “We promptly judged antique ivory buckles for the prize”
Output: No
Method-1: Without using STL
This approach is based on Hashing.
- A Hashing data structure of boolean type is created of size 26, such that index 0 represents the character ‘a’, 1 represents the character ‘b’ and so on.
- Traverse the string character by character and mark the particular character as present in the Hash.
- After complete traversal and marking of the string, traverse the Hash and see if all characters are present, i.e. every index has true. If all are marked, then return true, else False.
Below is the implementation of the above approach:
C++
// C++ Program to check if the given // string is a pangram or not #include <bits/stdc++.h> using namespace std; // Returns true if the string is // pangram else false bool checkPangram(string& str) { // Create a hash table to mark // the characters // present in the string vector<bool> mark(26, false); // For indexing in mark[] int index; // Traverse all characters for (int i = 0; i < str.length(); i++) { // If uppercase character, // subtract 'A' to find index. if ('A' <= str[i] && str[i] <= 'Z') index = str[i] - 'A'; // If lowercase character, // subtract 'a' to find index. else if ('a' <= str[i] && str[i] <= 'z') index = str[i] - 'a'; // If this character is not // an alphabet, skip to next one. else continue; mark[index] = true; } // Return false // if any character is unmarked for (int i = 0; i <= 25; i++) if (mark[i] == false) return (false); // If all characters were present return (true); } // Driver Code int main() { string str = "We promptly judged" " antique ivory" " buckles for the next prize"; if (checkPangram(str) == true) printf("Yes"); else printf("No"); return (0); } |
Yes
Time Complexity: O(N), where N is the length of the string.
Auxiliary Space: O(1)
Method-2: Using STL
The transform() method of STL can be used to check whether the given string is Pangram or not.
Syntax:
transform(s.begin(), s.end(), s.begin(), ::toupper);
Approach:
In order to check if the string contains all the alphabets of the English alphabet:
Step 1: Firstly convert all the letters into uppercase or lowercase because if it will check without converting, the lowercase and uppercase alphabets will be considered as different letters.
Step 2: Sort the string and check the distinct letter.
Step 3: Space will also be considered as a distinct entity.
Step 4: Now check if count =27 then the string contains all the 26 alphabets.
Below is the implementation of the above approach:
CPP
// C++ Program to check whether // a string pangram or not using STL #include <bits/stdc++.h> using namespace std; // Function to return given string // str is pangrams yes or no string pangrams(string s) { // Initialization of count int count = 0; // Convert each letter into // uppercase to avoid counting // of both uppercase and // lowercase as different letters transform(s.begin(), s.end(), s.begin(), ::toupper); // Sort the string sort(s.begin(), s.end()); // Count distinct alphabets for (int i = 0; i < s.size(); i++) { if (s[i] != s[i + 1]) count++; } // If count is 27 then the string // contains all the alphabets // including space as a // distinct character if (count == 27) return "Yes"; else return "No"; } // Driver code int main() { // Given string str string str = "We promptly " "judged antique" "ivory buckles for " "the next prize"; // Function Call cout << pangrams(str); return 0; } |
Yes
Time Complexity: O(N), where N is the length of the string.
Auxiliary Space: O(1)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Python program to check if given string is pangram
- Check if a String can be converted to Pangram in K changes
- Missing characters to make a string Pangram
- C Program to Check if a Given String is Palindrome
- Program to check if first and the last characters of string are equal
- Program to check if input is an integer or a string
- Program to check if the String is Empty in Java
- Program to check if a String in Java contains only whitespaces
- Program to check if the String is Null in Java
- Java program to check whether a string is a Palindrome
- Python program to check if a string is palindrome or not
- Python program to check if given string is vowel Palindrome
- Python program to check if a string contains all unique characters
- Pangram Checking
- Using Set() in Python Pangram Checking
- String slicing in Python to check if a string can become empty by recursive deletion
- Check if a string can be formed from another string by at most X circular clockwise shifts
- Check if a string can be converted to another string by replacing vowels and consonants
- Check if given string is a substring of string formed by repeated concatenation of z to a
- Check if any permutation of string is a K times repeated string
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

