ISC Practicals and Theory - Pangram (OOP)
Updated 21 Oct 2026
A pangram is a sentence that contains every letter of the English alphabet at least once. This version of the program is written as a class with a parameterised constructor: main reads the sentence and hands it to the constructor, and separate methods mark the letters, test them and print the result.
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 are the same letter; digits, spaces and punctuation are ignored. For example, The quick brown fox jumps over the lazy dog. is a pangram, while the same sentence with cog in place of dog is not, because d is missing.
Algorithm
mainreads the sentence intoswithnextLine(). Ifs.length()is 0, it printsINVALID INPUTand stops.- Otherwise
maincreatesob = new Pangram(s). The constructor storessinsentand createsseen[], 26 elements allfalse. mark()visits every characterchofsent:- A capital letter has 32 added to it to become the small letter.
- If
chis then between'a'and'z',seen[ch - 'a']is set totrue; other characters are ignored.
isPangram()returnsfalseas soon as it finds aseen[i]that isfalse, otherwisetrue.display()printsPANGRAMifisPangram()istrue. Otherwise it printsNOT A PANGRAMand callsshowMissing(), which printsMissing letters:and every letter whoseseen[i]isfalse, using(char)('a' + i).
Program
Compared with the plain version, the sentence comes in through the parameterised constructor Pangram(String s), there is no accept() method, and printing the missing letters has its own method showMissing().
import java.util.Scanner;
/* Class Pangram (OOP version): the sentence is given to the constructor. */
public class Pangram
{
String sent; // stores the sentence
boolean seen[]; // seen[0] is for 'a' ... seen[25] for 'z'
/* Parameterised constructor. Parameter s: the sentence to be checked.
Stores it in sent and creates the seen[] array (all false). */
Pangram(String s)
{
sent = s;
seen = new boolean[26];
}
/* Marks in seen[] every letter present in sent, using character
arithmetic (no Character.isLetter() or toLowerCase()). */
void mark()
{
int i;
char ch;
for (i = 0; i < sent.length(); i++) // visit every character
{
ch = sent.charAt(i);
if (ch >= 'A' && ch <= 'Z') // capital: bring to the small-letter range
ch = (char)(ch + 32);
if (ch >= 'a' && ch <= 'z') // ignore digits, spaces, punctuation
seen[ch - 'a'] = true; // 'a' -> 0 ... 'z' -> 25
}
}
/* Returns true if all 26 letters have been seen, otherwise false */
boolean isPangram()
{
int i;
for (i = 0; i < 26; i++)
{
if (seen[i] == false) // one missing letter is enough to fail
return false;
}
return true;
}
/* Prints the names of the letters that are missing (after the
heading "Missing letters: ") */
void showMissing()
{
int i;
System.out.print("Missing letters: ");
for (i = 0; i < 26; i++)
{
if (seen[i] == false)
System.out.print((char)('a' + i) + " "); // index back to letter
}
System.out.println();
}
/* Prints the result; for a non-pangram it also calls showMissing() */
void display()
{
if (isPangram())
System.out.println("PANGRAM");
else
{
System.out.println("NOT A PANGRAM");
showMissing();
}
}
/* main reads the sentence, checks it is not empty, creates the object
with the sentence and calls the methods. */
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter a sentence: ");
String s = sc.nextLine();
if (s.length() == 0) // an empty line is the only invalid input
System.out.println("INVALID INPUT");
else
{
Pangram ob = new Pangram(s); // object of class Pangram
ob.mark();
ob.display();
}
}
}Variable description table
| Variable | Data Type | Purpose |
|---|---|---|
sent | String | Data member: the sentence to be checked |
seen | boolean[] | Data member: seen[0] to seen[25] tell whether a to z has occurred |
s | String | Parameter of the constructor, and the line read in main |
i | int | Loop counter in mark(), isPangram() and showMissing() |
ch | char | The character of sent being examined in mark() |
sc | Scanner | Reads the sentence from the keyboard in main |
args | String[] | Command line arguments of main (not used) |
ob | Pangram | Object created in main to call the methods |
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 mark() word by word. A letter 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. isPangram() finds seen[3] (the letter d) is false and returns false. display() prints NOT A PANGRAM, and showMissing() prints Missing letters: d, 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."