Pangram Java Program ISC Computer Science 2025 Practical

Write a program to accept a sentence which may be terminated by either '.', '?' or '!' only. The words may be separated by a single blank space and should be case-sensitive.

Perform the following tasks:
(a) Determine if the accepted sentence is a Pangram or not. A Pangram is a sentence that contains every letter of the alphabet at least once. Example: "The quick brown fox jumps over the lazy dog."
(b) Display the first occurring longest and shortest word in the accepted sentence.

https://youtu.be/6_vaJuM-rSQ

Test your program for the following data and some random data:

Example 1
INPUT: Pack my box with five dozen liquor jugs.
OUTPUT:
IT IS A PANGRAM
LONGEST WORD: liquor
SHORTEST WORD: my

Example 2
INPUT: THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG.
OUTPUT:
IT IS A PANGRAM
LONGEST WORD: QUICK
SHORTEST WORD: THE

Example 3
INPUT: Hello my World.
OUTPUT:
IT IS NOT A PANGRAM
LONGEST WORD: Hello
SHORTEST WORD: my

Example 4
INPUT: Alas! it failed#
OUTPUT: INVALID INPUT

import java.util.Scanner;
class Pangram{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        System.out.print("Enter the sentence: ");
        String s = in.nextLine();
        if(s.length() == 0){
            System.out.println("INVALID INPUT");
            return;
        }
        char last = s.charAt(s.length() - 1);
        if(".?!".indexOf(last) == -1){
            System.out.println("INVALID INPUT");
            return;
        }
        String shortest = new String(s);
        String longest = new String();
        boolean isPangram = true;
        for(char ch = 'a'; ch <= 'z'; ch++){
            boolean match = false;
            for(int i = 0; i < s.length(); i++){
                if(ch == Character.toLowerCase(s.charAt(i))){
                    match = true;
                    break;
                }
            }
            if(!match){
                isPangram = false;
                break;
            }
        }
        if(isPangram)
            System.out.println("IT IS A PANGRAM");
        else
            System.out.println("IT IS NOT A PANGRAM");
        String word = new String();
        for(int i = 0; i < s.length(); i++){
            char ch = s.charAt(i);
            if(Character.isLetterOrDigit(ch))
                word += ch;
            else{
                if(word.length() > longest.length())
                    longest = new String(word);
                if(word.length() < shortest.length())
                    shortest = new String(word);
                word = new String();
            }
        }
        System.out.println("LONGEST WORD: " + longest);
        System.out.println("SHORTEST WORD: " + shortest);
    }
}

Comments