Question 5

Code for Pangram Checking

Pangram Checking

Given a string check if it is Pangram or not. A pangram is a sentence containing every letter in the English Alphabet.

Examples : The quick brown fox jumps over the lazy dog ” is a Pangram [Contains all the characters from ‘a’ to ‘z’]
“The quick brown fox jumps over the dog” is not a Pangram [Doesn’t contains all the characters from ‘a’ to ‘z’, as ‘l’, ‘z’, ‘y’ are missing]

We create a mark[] array of Boolean type. We iterate through all the characters of our string and whenever we see a character we mark it. Lowercase and Uppercase are considered the same. So ‘A’ and ‘a’ are marked in index 0 and similarly ‘Z’ and ‘z’ are marked in index 25.

After iterating through all the characters we check whether all the characters are marked or not. If not then return false as this is not a pangram else return true.

Code for checking whether a string is Pangram or not

// C Program to check the given string is a pangram or not
#include<stdio.h>
int main()
{
    char str[120];
    int i,alphabet[26]={0},result=0;
     
    printf("Enter the String: ");
    scanf("%s",str);
     
    for(i=0;str[i]!='\0';i++)
    {
        if('a'<=str[i] && str[i]<='z')
        {
            result+=!alphabet[str[i]-'a'];
            alphabet[str[i]-'a']=1;
        }
        else if('A'<=str[i] && str[i]<='Z')
        {
            result+=!alphabet[str[i]-'A'];
            alphabet[str[i]-'A']=1;
        }
    }
     
    if(result==26)
    {
        printf("The String is a Pangram");
    }
    else
    {
        printf("The String is not a Pangram");
    }
    return 0;
}
Output
Enter the String: qwertyiopasdfghjklzxcvbnm
The String is a Pangram
// Java Program to check whether a string is Panagram or not
import java.util.*;
class Main
{

// Returns true if the string is pangram else false
public static boolean checkPangram (String str)
  {

// Create a hash table to mark the characters present in the string
// By default all the elements of mark would be false.
    boolean[]mark = new boolean[26];
    int index = 0;

// Traverse all characters
    for (int i = 0; i < str.length (); i++)
      {
// If uppercase character, subtract 'A'to find index.
	if ('A' <= str.charAt (i) && str.charAt (i) <= 'Z')

	  index = str.charAt (i) - 'A';

// If lowercase character, subtract 'a'to find index.
	else if ('a' <= str.charAt (i) && str.charAt (i) <= 'z')

	  index = str.charAt (i) - 'a';

// Mark current character
	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);
  }

// Main Code
  public static void main (String[]args)
  {
      Scanner sc=new Scanner(System.in);
      System.out.println ("Enter a string");
      String str=sc.next();
    if (checkPangram (str) == true)
      System.out.print (str + " is a pangram.");
    else
      System.out.print (str + " is not a pangram.");

  }
}
Output
Enter a string
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy dog is a pangram

15 comments on “Question 5”


  • Adit

    import java.util.Scanner;
    public class Prog61
    {
    static int flag=0;
    static int check(char ch)
    {

    for(int i=0;i<26;i++)
    {
    if(ch==(char)(i+97) || ch==(char)(i+65))
    {
    flag=1;
    break;
    }
    else
    {
    flag=0;
    }
    }
    if(flag==0)
    {
    return 0;
    }
    else
    {
    return 1;
    }
    }
    public static void main(String args[])
    {
    Scanner sc=new Scanner(System.in);
    String strNew=sc.nextLine();
    String str1=strNew.replaceAll("//s","");
    boolean var=true;
    char str[]=str1.toCharArray();
    int count=0;
    int arr[]=new int[40];
    for(int i=0;i<26;i++)
    {
    arr[i]=0;
    }

    for(int i=0;i<str.length;i++)
    {
    if(str[i]=’a’ || str[i]>=’A’ && str[i]<='Z'){
    arr[count++]=check(str[i]);
    }
    else
    {
    i++;
    }
    }

    //checking for pangram
    for(int i=0;i<count;i++)
    {
    if(arr[i]==1)
    {
    var=true;
    }
    else{
    var=false;

    System.out.print("Not Pangram String");
    break;
    }
    }
    if(var==true)
    {
    System.out.print("Pangram String");
    }
    }

    }


  • Piyush

    //c++ code for the same….o(n)
    #include

    #include

    using namespace std;

    int main()
    {

    string str = “The quick brown fox jumps over the lzy dog”;

    int arr[26];

    for( int i=0;i<26;i++){
    arr[i]=0; }

    for(int i=0;i=65 && x=97&&x<123)){
    arr[x-97]=1;
    }

    }
    int x=0;
    for( int i=0;i<26;i++){
    if(arr[i]==1){
    x++;
    }
    }

    if (x==26){
    cout<<"pangram";
    }
    else{
    cout<<"not";
    }

    return 0;
    }