Check Pangram String

Check Pangram String” is a very popular and basic problem of string data structure, asked in many technical interviews. Here, we are given a string of length ‘n’ and our task is to check whether given string is panagram string or not.

Panagram String: A string is called Panagram String when it contains all the alphabets, irrespective of capitalization.

Example (Check Pangram String):

INPUT:
Qwertyuiopasdfghjklzxcvbnm
OUTPUT:
The given string is a panagram string.

The steps required to check pangram string or not are as follows:

  1. Create an hash array of size 123 and initialize it with 0. ( 123 size is taken because the ascii value of A-Z is from 65 to 90 and ascii value of a-z is from 97 to 122).
  2. For every alphabet encountered, increment the hash array value for both small and capital alphabet ascii value index.
  3. Finally, iterate over hash array from 65 to 90, if any value is zero, then string is not pangram, else string is pangram.

The time complexity of this solution is O(n), for scanning the length of string.

C++ Program to check pangram string is as follows:

/*C++ Program to check panagram string*/  
#include<bits/stdc++.h>  
#define size 1000  
using namespace std;  
int main()  
{  
    string str;  
    cout<<"Enter a string:\n";  
    getline(cin,str);  
    int flag1 = 0;  
    /* Create a Hash Array */  
    int hash[123] = {0};  
    /* For every alphabet, increment the  
       hash-array value for that character */  
    for(int i = 0; i <  str.length(); i++)  
    {  
        if(str[i]>='a' && str[i]<='z')  
        {  
            hash[str[i]]++; /* Increment for small alphabet character */  
            hash[str[i]-32]++; /* Increment for capital alphabet character */  
        }  
        else if(str[i]>='A' && str[i]<='Z')  
        {  
            hash[str[i]]++; /* Increment for Capital alphabet character */  
            hash[str[i]+32]++; /* Increment for small alphabet character */  
        }  
    }  
    /* Check if any hash array value from 65 to 90 is 0 or not */  
    for(int i = 65; i <= 90; i++)  
    {  
        /* If any hash-array value is 0, then  
        that string is not panagram*/  
        if(hash[i] == 0 || hash[i+32] == 0)  
        {  
            flag1 = 1;  
            cout<<"\nThe given string is not pangram";  
            break;  
        }  
    }  
    if(flag1 == 0)  
    {  
        cout<<"\nThe given string is pangram";  
    }  
}  
OUTPUT:
Enter a string: qwertyuiopasdfghjklzxcvbnm
The given string is panagram

Related Posts:

  1. Find First Non-Repeating Character of the String.
  2. Find Most Frequent Element of the Array.
  3. Find Pair in an Array with Given Sum.
  4. Find First Repeating Element of an Array.
  5. Find Majority Element of an Array.
  6. Find Most Frequent Character of the String.
  7. Check Anagram Strings.
  8. Check Whether Given String is Palindromic Anagram or Not.
  9. Program to count occurrences of a particular element in an array.
  10. Program to search an element in an Array (Linear Search).
  11. Program to Rotate Array Elements by ‘D’ positions.
  12. Program to find most frequent element in an array.
  13. Program to find pair in an array with given sum.
  14. Program to find first repeating element of an array.
  15. Program to merge two sorted arrays.
  16. Program to find missing number in an array.
  17. Program to sort if array is sorted.
  18. Program to print Alternate Elements of an Array.
  19. Program to swap kth element from beginning to kth element from end in an Array.
  20. Program to print all possible subarrays of the given array.

You may also like...