Wednesday 25 October 2017

Pangrams Hacker rank solution in C


 #include <stdio.h>
int main()
{
               char st[1000],alpha;
               int i,flag;
               scanf("%[^\n]",st);
               for(alpha='a'; alpha <= 'z';alpha++)
               {
                              flag=0;
                              for(i=0;st[i]!='\0';i++)
                              {
                                             if(st[i]== alpha || st[i]== alpha-32)//in entire string if ‘a’ is present
                                             {
                                                            flag=1;
                                                            break;
                                             }
                              }
                              if(flag == 0) //in entire string ‘a’ is not present
                                             break;
               }
               if(flag == 0)
                              printf("not pangram");
               else
                              printf("pangram");
    return 0;
}

Output:

3 comments:

  1. why should we take ,
    st[i]==alpha-32;
    can u plz explain this??

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete
    2. if the input is like this AbCDefGHiJklmnOPQRSTUVwxYZ
      then also it is a pangram.
      st[i] == 'a' will only check the presence of 'a'. to check the presence of 'A' we use st[i] == 'a'-32
      ascii value of 'a' = 97 and 97-32 = 65 and 65 is ascii value of 'A'

      Delete