Given a string check if it is Pangram or not. A pangram is a sentence containing every letter in the English Alphabet.
Examples : The quick brown fox jumps over the lazy dog ” is a Pangram [Contains all the characters from ‘a’ to ‘z’]“The quick brown fox jumps over the dog” is not a Pangram [Doesn’t contains all the characters from ‘a’ to ‘z’, as ‘l’, ‘z’, ‘y’ are missing]
We create a mark[] array of Boolean type. We iterate through all the characters of our string and whenever we see a character we mark it. Lowercase and Uppercase are considered the same. So ‘A’ and ‘a’ are marked in index 0 and similarly ‘Z’ and ‘z’ are marked in index 25.
After iterating through all the characters we check whether all the characters are marked or not. If not then return false as this is not a pangram else return true.
// A 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'; // Mark current character 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 Program to test above functions int main() { string str = "The quick brown fox jumps over the" " lazy dog"; if (checkPangram(str) == true) printf ("/"%s/" is a pangram", str.c_str()); else printf ("/"%s/" is not a pangram", str.c_str()); return(0); }
Output :
"The quick brown fox jumps over the lazy dog" is a pangram
Time Complexity: O(n), where n is the length of our stringAuxiliary Space – O(1).
This article is contributed by Rachit Belwariar . If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
转载本站任何文章请注明:转载至神刀安全网,谢谢神刀安全网 » Pangram Checking