/ Assignment / assignment_26.java
assignment_26.java
1 /* 2 * Write code to check if a given sentence is a pangram i.e it contains all the letters of the alphabet 3 */ 4 import java.util.Scanner; 5 6 public class assignment_26 { 7 8 // Method to check if the given sentence is a pangram 9 public static boolean isPangram(String sentence) { 10 int letterMask = 0; // Creates a 32-bit integer to track presence of letters using bits 11 12 // Iterate through each character in the sentence 13 for (char ch : sentence.toCharArray()) { // Loops through each character in the string 14 char lower = Character.toLowerCase(ch); // Converts current character to lowercase 15 16 // Check if the character is a letter 17 if(lower >= 'a' && lower <= 'z') { // Verifies if character is a letter from 'a' to 'z' 18 int bitPos = lower - 'a'; // Calculates bit position (0-25) based on letter's position in alphabet 19 letterMask = letterMask | (1 << bitPos); // Sets the bit at calculated position using bitwise OR 20 } 21 } 22 // Check if all 26 bits are set 23 return (letterMask == Math.pow(2, 26) - 1); // Returns true if all 26 bits are set (compares with 2^26-1) 24 } 25 26 // Main method to execute the program 27 public static void main(String[] args) { 28 Scanner sc = new Scanner(System.in); // Initializes Scanner object to read user input 29 30 System.out.println("Enter a sentence: "); // Prompts user to enter a sentence 31 String sentence = sc.nextLine(); // Stores the entire line of user input as a string 32 33 // Print whether the sentence is a pangram 34 boolean is_pangram = isPangram(sentence); // Calls isPangram method and stores the result 35 36 if (is_pangram) { // Checks if the sentence is a pangram 37 System.out.println("It is a pangram"); // Prints confirmation if all letters are present 38 } else { // Executes if sentence is not a pangram 39 System.out.println("It is not a pangram"); // Prints message if some letters are missing 40 } 41 } 42 }