0% found this document useful (0 votes)
114 views5 pages

Project 17 F

The document outlines a programming task to create a Pangram checker that determines if a given sentence is a Pangram, identifies the longest and shortest words, and handles invalid input. The program must accept a sentence ending with '.', '?', or '!', and process it to check for the presence of all letters of the alphabet. It includes an algorithm and Java code implementation to achieve these functionalities.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
114 views5 pages

Project 17 F

The document outlines a programming task to create a Pangram checker that determines if a given sentence is a Pangram, identifies the longest and shortest words, and handles invalid input. The program must accept a sentence ending with '.', '?', or '!', and process it to check for the presence of all letters of the alphabet. It includes an algorithm and Java code implementation to achieve these functionalities.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

PROJECT 17

QUESTION
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-insensitive.

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.

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

Example 1

Page No: 5

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


ALGORITHM

Step 1: Start.

Step 2: Accept a sentence from the user.

Step 3: Check the last character of the sentence:

 If it is not '.', '?', or '!', display:


"INVALID INPUT"
and terminate the program.

Step 4: Remove the last character from the sentence.

Step 5: Convert the sentence to lowercase to make it case-insensitive.

Step 6: Initialize a boolean array alphabet[26] to keep track of which alphabets (a–z) are present.

Step 7: Traverse each character of the sentence:

 If the character is between 'a' and 'z', mark alphabet[ch - 'a'] = true.

Step 8: Check the alphabet[] array:

 If all 26 entries are true, print: "IT IS A PANGRAM".


 Else, print: "IT IS NOT A PANGRAM".

Step 9: Split the sentence into words using a single space " ".

Step 10: Initialize two variables longest and shortest to the first word.

Step 11: Traverse through all words:

 If a word’s length is greater than longest.length(), update longest.


 If a word’s length is smaller than shortest.length(), update shortest.

Step 12: Display:

 "LONGEST WORD: <longest>"


 "SHORTEST WORD: <shortest>"

Step 13: End.

CODING
import java.util.*;

public class PangramChecker {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("Enter a sentence (ending with '.', '?', or '!'):");


String input = sc.nextLine().trim();

char lastChar = input.charAt(input.length() - 1);

if (lastChar != '.' && lastChar != '?' && lastChar != '!') {

System.out.println("INVALID INPUT");

return;

input = input.substring(0, input.length() - 1).toLowerCase();

boolean[] alphabet = new boolean[26];

for (char ch : input.toCharArray()) {

if (ch >= 'a' && ch <= 'z') {

alphabet[ch - 'a'] = true;

boolean isPangram = true;

for (boolean letterPresent : alphabet) {

if (!letterPresent) {

isPangram = false;

break;

if (isPangram) {

System.out.println("IT IS A PANGRAM");

} else {

System.out.println("IT IS NOT A PANGRAM");

String[] words = input.split(" ");

String longest = "", shortest = "";


if (words.length > 0) {

longest = shortest = words[0];

for (String word : words) {

if (word.length() > longest.length()) {

longest = word;

if (word.length() < shortest.length()) {

shortest = word;

System.out.println("LONGEST WORD: " + longest);

System.out.println("SHORTEST WORD: " + shortest);

OUTPUT
VARIABLE DESCRIPTION
Variable Name Data Type Purpose / Description

sc Scanner Used to accept input from the user.

input String Stores the original sentence entered by the user.

lastChar char Stores the last character of the sentence to check for valid termination.

alphabet boolean[] Array of size 26 to track whether each letter from 'a' to 'z' is present in the sentence.

isPangram boolean Flag to indicate whether the sentence is a pangram or not.

words String[] Array that holds all the words in the sentence after splitting.

word String Temporary variable used while iterating through words.

longest String Stores the first occurring longest word in the sentence.

shortest String Stores the first occurring shortest word in the sentence.

You might also like