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 letterdis missing.PACK MY BOX WITH 5 DOZEN LIQUOR JUGS!!is not: the digit5replaced the word FIVE, sofandvare missing.
Algorithm
- Read the sentence into
sent. If it is empty, printINVALID INPUTand stop. - Create the
booleanarrayseenof size 26, allfalse.seen[0]stands for a,seen[1]for b, and so on up toseen[25]for z. - Visit every character
chof 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', setseen[ch - 'a'] = true. Digits, spaces and punctuation marks are ignored. Only character arithmetic is used, notCharacter.isLetter()ortoLowerCase(). - Set
pan = trueand go throughseenfrom 0 to 25; onefalseis enough to setpan = false. - If
panis true, printPANGRAM. Otherwise printNOT A PANGRAMand then, going throughseenagain, print the letter(char) ('a' + i)for everyiwithseen[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
| Variable | Data Type | Purpose |
|---|---|---|
args | String[] | Command line arguments of main; not used |
sc | Scanner | Reads the input typed by the user |
sent | String | The sentence typed by the user |
seen | boolean[] | seen[0] is true if a was found, ... seen[25] if z was found |
i | int | Loop counter over the characters or over the 26 letters |
ch | char | The character being examined, changed to a small letter if it is a capital |
pan | boolean | Stays 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.
PANGRAMSample 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 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.
| Word | Letters newly set to true | Letters set so far |
|---|---|---|
The | t, h, e | 3 |
quick | q, u, i, c, k | 8 |
brown | b, r, o, w, n | 13 |
fox | f, x (o is already set) | 15 |
jumps | j, m, p, s (u is already set) | 19 |
over | v | 20 |
the | none | 20 |
lazy | l, a, z, y | 24 |
cog | g | 25 |
. | ignored | 25 |
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
- Anagram "Write a program to check whether two words or phrases are anagrams of each other."
- Letter frequency "Write a program to accept a sentence and print the frequency of each letter in it."
- Isogram "Write a program to check whether a word has no repeated letter."