Check whether given string is Panagram String or not

Check whether given string is Panagram String or not” 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:

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

The steps required to check whether the string is panagram 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 panagram, else string is panagram.

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

C++ Program to check whether string is panagram or not is as follows:

/*C++ Program to check if string is panagram or not*/  
#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 panagram";  
            break;  
        }  
    }  
    if(flag1 == 0)  
    {  
        cout<<"\nThe given string is panagram";  
    }  
}  
OUTPUT:
Enter a string: qwertyuiopasdfghjklzxcvbnm
The given string is panagram

Related Posts:

  1. Program to find first non-repeating character of the String.
  2. Program to Reverse a String.
  3. Program to Count Number of Characters in a String.
  4. Program to Count frequency of particular character of the String.
  5. Program to Count Number of Vowels and Number of Consonants in a String.
  6. Program to Check if String is alphabetic or numeric or alpha-numeric.
  7. Program to copy one String to Another.
  8. Program to Concatenate Two Strings.
  9. Program to Check whether given String is Palindrome or not.
  10. Program to convert lower-case alphabet to upper-case alphabet and vice-versa.
  11. Program to count total number of words in a string.
  12. Program to merge two sorted arrays.
  13. Program to find missing number in an array.
  14. Program to sort if array is sorted.
  15. Program to print Alternate Elements of an Array.
  16. Program to swap kth element from beginning to kth element from end in an Array.
  17. Program to print all possible subarrays of the given array.
  18. Program to print kth smallest and kth largest element of an Array.
  19. Program to find equilibrium index of an Array.
  20. Program to find majority element of an Array.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *