Prashnikaप्रश्निका
Code
About
‹ All Computer Science notes

ISC Practicals and Theory - Pangram

Updated 21 Oct 2026

A pangram is a sentence that uses every letter of the English alphabet at least once. This program, written in the main method alone, reads a sentence, marks the letters it contains in a boolean array of size 26, and prints whether the sentence is a pangram. If it is not, it also prints the letters that are missing.

Definition

A sentence is a pangram if each of the 26 letters a to z occurs in it at least once. Capital and small letters count as the same letter, and digits, spaces and punctuation marks are ignored.

  • The quick brown fox jumps over the lazy dog. is a pangram: all 26 letters occur.
  • The quick brown fox jumps over the lazy cog. is not: the letter d is missing.
  • PACK MY BOX WITH 5 DOZEN LIQUOR JUGS!! is not: the digit 5 replaced the word FIVE, so f and v are missing.

Algorithm

  1. Read the sentence into sent. If it is empty, print INVALID INPUT and stop.
  2. Create the boolean array seen of size 26, all false. seen[0] stands for a, seen[1] for b, and so on up to seen[25] for z.
  3. Visit every character ch of the sentence. If it is a capital letter, add 32 to bring it to the small-letter range. If it is now between 'a' and 'z', set seen[ch - 'a'] = true. Digits, spaces and punctuation marks are ignored. Only character arithmetic is used, not Character.isLetter() or toLowerCase().
  4. Set pan = true and go through seen from 0 to 25; one false is enough to set pan = false.
  5. If pan is true, print PANGRAM. Otherwise print NOT A PANGRAM and then, going through seen again, print the letter (char) ('a' + i) for every i with seen[i] == false.

Program

The first for loop in main marks the letters, the second decides whether all are present, and the last lists the missing ones.

import java.util.Scanner;

/* Program Pangram checks whether a sentence contains every letter of the
   English alphabet at least once (capital or small letters) and lists the
   missing letters when it does not. */
public class Pangram
{
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter a sentence: ");
        String sent = sc.nextLine();             // the sentence entered by the user
        if (sent.length() == 0)
            System.out.println("INVALID INPUT");
        else
        {
            boolean seen[] = new boolean[26];    // seen[0] is for 'a', seen[1] for 'b' ... seen[25] for 'z'
            for (int i = 0; i < sent.length(); i++)   // visit every character
            {
                char ch = sent.charAt(i);
                if (ch >= 'A' && ch <= 'Z')      // capital letter: bring it to the small-letter range
                    ch = (char) (ch + 32);
                if (ch >= 'a' && ch <= 'z')      // digits, spaces and punctuation are ignored
                    seen[ch - 'a'] = true;       // 'a' -> 0 ... 'z' -> 25
            }
            boolean pan = true;                  // stays true if every letter has been seen
            for (int i = 0; i < 26; i++)
            {
                if (seen[i] == false)            // one missing letter is enough to fail
                    pan = false;
            }
            if (pan)
                System.out.println("PANGRAM");
            else
            {
                System.out.println("NOT A PANGRAM");
                System.out.print("Missing letters: ");
                for (int i = 0; i < 26; i++)
                {
                    if (seen[i] == false)
                        System.out.print((char) ('a' + i) + " ");   // index back to letter
                }
                System.out.println();
            }
        }
    }
}

Variable description table

VariableData TypePurpose
argsString[]Command line arguments of main; not used
scScannerReads the input typed by the user
sentStringThe sentence typed by the user
seenboolean[]seen[0] is true if a was found, ... seen[25] if z was found
iintLoop counter over the characters or over the 26 letters
chcharThe character being examined, changed to a small letter if it is a capital
panbooleanStays true while every letter has been seen

Sample input and output

Sample 1

Enter a sentence: The quick brown fox jumps over the lazy dog.
PANGRAM

Sample 2

Enter a sentence: The quick brown fox jumps over the lazy cog.
NOT A PANGRAM
Missing letters: d 

Sample 3

Enter a sentence: PACK MY BOX WITH 5 DOZEN LIQUOR JUGS!!
NOT A PANGRAM
Missing letters: f v 

One value per line

Dry run

Input The quick brown fox jumps over the lazy cog. The table shows the marking loop word by word. A letter that is already true stays true.

WordLetters newly set to trueLetters set so far
Thet, h, e3
quickq, u, i, c, k8
brownb, r, o, w, n13
foxf, x (o is already set)15
jumpsj, m, p, s (u is already set)19
overv20
thenone20
lazyl, a, z, y24
cogg25
.ignored25

Only 25 letters are set. The check loop scans seen[] from index 0 and finds seen[3], the letter d, is false, so pan becomes false. The program prints NOT A PANGRAM, and in the loop that lists the missing letters only seen[3] is false, so it prints Missing letters: d. This is the same as the real run above.

Variations

  1. Anagram "Write a program to check whether two words or phrases are anagrams of each other."
  2. Letter frequency "Write a program to accept a sentence and print the frequency of each letter in it."
  3. Isogram "Write a program to check whether a word has no repeated letter."