Given string str, the task is to check whether a string is pangram or not using in C++.

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.

  1. 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.
  2. Traverse the string character by character and mark the particular character as present in the Hash.
  3. 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++ 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); 
}

#c++ #hash #strings #stl #programming-c #cplusplus

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