C Program to check given String is Pangram or not

Before going to the program first let us understand what is Pangram?

Pangram:

          A Pangram is a sentence containing every letter of the alphabet.

Example: Let us consider a sentence as given below:

The quick brown fox jumps over the lazy dog.

 The above sentence containing every letter of the alphabet.

Thus the Entered String is a Pangram String.

Now let us see the program code to check given String is Pangram or not and understand the code using the Explanation given below.

Program code to check given String is Pangram or not:

#include<stdio.h>
#include<conio.h>

void main()
{
    char s[100];
    int i,used[26]={0},total=0;
    clrscr();
    
    printf("\n Enter the String:\n");
    gets(s);
    
    for(i=0;s[i]!='\0';i++)
    {
        if('a'<=s[i] && s[i]<='z')
        {
            total+=!used[s[i]-'a'];
            used[s[i]-'a']=1;
        }
        else if('A'<=s[i] && s[i]<='Z')
        {
            total+=!used[s[i]-'A'];
            used[s[i]-'A']=1;
        }
    }
    
    if(total==26)
    {
        printf("\n The Entered String is a Pangram String.");
    }
    else
    {
        printf("\n The Entered String is not a Pangram String.");
    }
    getch();
}

Explanation:

  • First the computer reads the strings and stores it in the “s” array variable using the following lines:
printf("\n Enter the String:\n");
gets(s);
  • Then using the for loop it reads character by character of the String and if character is an alphabet then increment the total if used[alphabet]=0 and it make used[alphabet]=1 using the following lines:
for(i=0;s[i]!='\0';i++)
{
    if('a'<=s[i] && s[i]<='z')
    {
        total+=!used[s[i]-'a'];
        used[s[i]-'a']=1;
    }
    else if('A'<=s[i] && s[i]<='Z')
    {
        total+=!used[s[i]-'A'];
        used[s[i]-'A']=1;
    }
}

Note: ‘\0’ is the NULL character used to represent the End the String.

total+=!used[s[i]-‘a’]   means  total=total+(!used[s[i]-‘a’]) 

!used[s[i]-‘a’]  returns 1 if used[s[i]-‘a’]=0 and vice versa. 

  • Finally if-else condition is used to print the String is Pangram or not on the screen using the following lines:
if(total==26)
{
    printf("\n The Entered String is a Pangram String.");
}
else
{
    printf("\n The Entered String is not a Pangram String.");
}

Step by Step working of the above Program Code:

  1. Let us assume that a user enters the String as “The five boxing wizards jump quickly.”
  2. So it stores the Strings in s[]=The five boxing wizards jump quickly. and also assigns total=0 and used[26]={0}.

Step for calculating the total:

  1. Then it assigns i=0 and the loop continues till the condition of for loop is true.

3.1.   s[0] != ‘\0’   (‘T‘!=’\0’)    for loop condition is true

‘a'<=s[0] && s[0]<=’z’   (‘a'<=’T’ && ‘T'<=’z’)   if condition is false

‘A'<=s[0] && s[0]<=’Z’   (‘A'<=’T’ && ‘T'<=’Z’)   else-if condition is true

total+=!used[s[0]-‘A’]    (total=total+(!used[‘T’-‘A’]))   So, total=0+(!0)=1

used[‘T’-‘A’]=1    So,  used[19]=1

i++    (i=i+1)    So,  i=1

3.2.  s[1] != ‘\0’   (‘h‘!=’\0’)    for loop condition is true

‘a'<=s[1] && s[1]<=’z’   (‘a'<=’h’ && ‘h'<=’z’)   if condition is true

total+=!used[s[1]-‘a’]    (total=total+(!used[‘h’-‘a’]))   So, total=1+(!0)=2

used[‘h’-‘a’]=1    So,  used[7]=1

i++    (i=i+1)    So,  i=2

3.3.  s[2] != ‘\0’   (‘e‘!=’\0’)    for loop condition is true

‘a'<=s[2] && s[2]<=’z’   (‘a'<=’e’ && ‘e'<=’z’)   if condition is true

total+=!used[s[2]-‘a’]    (total=total+(!used[‘e’-‘a’]))   So, total=2+(!0)=3

used[‘e’-‘a’]=1    So,  used[4]=1

i++    (i=i+1)    So,  i=3

3.4.  s[3] != ‘\0’   (‘ ‘!=’\0’)    for loop condition is true

‘a'<=s[3] && s[3]<=’z’   (‘a'<=’ ‘ && ‘ ‘<=’z’)   if condition is false

‘A'<=s[3] && s[3]<=’Z’   (‘A'<=’ ‘ && ‘ ‘<=’Z’)   else-if condition is false

i++    (i=i+1)    So,  i=4

3.5.  s[4] != ‘\0’   (‘f‘!=’\0’)    for loop condition is true

‘a'<=s[4] && s[4]<=’z’   (‘a'<=’f’ && ‘f'<=’z’)   if condition is true

total+=!used[s[4]-‘a’]    (total=total+(!used[‘f’-‘a’]))   So, total=3+(!0)=4

used[‘f’-‘a’]=1    So,  used[5]=1

i++    (i=i+1)    So,  i=5

3.6.  s[5] != ‘\0’   (‘i‘!=’\0’)    for loop condition is true

‘a'<=s[5] && s[5]<=’z’   (‘a'<=’i’ && ‘i'<=’z’)   if condition is true

total+=!used[s[5]-‘a’]    (total=total+(!used[‘i’-‘a’]))   So, total=4+(!0)=5

used[‘i’-‘a’]=1    So,  used[8]=1

i++    (i=i+1)    So,  i=6

3.7.  s[6] != ‘\0’   (‘v‘!=’\0’)    for loop condition is true

‘a'<=s[6] && s[6]<=’z’   (‘a'<=’v’ && ‘v'<=’z’)   if condition is true

total+=!used[s[6]-‘a’]    (total=total+(!used[‘v’-‘a’]))   So, total=5+(!0)=6

used[‘v’-‘a’]=1    So,  used[21]=1

i++    (i=i+1)    So,  i=7

3.8.  s[7] != ‘\0’   (‘e‘!=’\0’)    for loop condition is true

‘a'<=s[7] && s[7]<=’z’   (‘a'<=’e’ && ‘e'<=’z’)   if condition is true

total+=!used[s[7]-‘a’]    (total=total+(!used[‘e’-‘a’]))   So, total=6+(!1)=6

used[‘e’-‘a’]=1    So,  used[4]=1

i++    (i=i+1)    So,  i=8

3.9.  Similarly it continues till it reaches ‘\0’ and calculates the “total”.

3.10.   s[36] != ‘\0’   (‘\0′!=’\0’)    for loop condition is false

It comes out of the for loop.

Step for printing the Strings are Anagram or not:

  1. Finally using if-else condition it prints Pangram or not.

4.1.   total==26    (26==26)    if condition is true

So it prints The Entered String is a Pangram Strings.

Note: If the if condition is false,

then, it prints The Entered String is not a Pangram Strings.

  1. Thus program execution is completed.

Output:

pangram

pangram1

TO DOWNLOAD THE PROGRAM CODE : CLICK HERE

 

1 Response

  1. sk.sartaj says:

    Tq u soo much,its a very good explanation.

Leave a Reply

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