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

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

  1. main reads the sentence into s with nextLine(). If s.length() is 0, it prints INVALID INPUT and stops.
  2. Otherwise main creates ob = new Pangram(s). The constructor stores s in sent and creates seen[], 26 elements all false.
  3. mark() visits every character ch of sent:
    • A capital letter has 32 added to it to become the small letter.
    • If ch is then between 'a' and 'z', seen[ch - 'a'] is set to true; other characters are ignored.
  4. isPangram() returns false as soon as it finds a seen[i] that is false, otherwise true.
  5. display() prints PANGRAM if isPangram() is true. Otherwise it prints NOT A PANGRAM and calls showMissing(), which prints Missing letters: and every letter whose seen[i] is false, 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

VariableData TypePurpose
sentStringData member: the sentence to be checked
seenboolean[]Data member: seen[0] to seen[25] tell whether a to z has occurred
sStringParameter of the constructor, and the line read in main
iintLoop counter in mark(), isPangram() and showMissing()
chcharThe character of sent being examined in mark()
scScannerReads the sentence from the keyboard in main
argsString[]Command line arguments of main (not used)
obPangramObject 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.
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 mark() word by word. A letter 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. 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

  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."