Check Pangram in Java

Check whether a sentence is a pangram (contains all letters A–Z).

IntermediateModule 4: String ProgramsExample 22 of 25
check-pangram.java
1import java.util.HashSet;
2import java.util.Scanner;
3import java.util.Set;
4
5public class Main {
6 public static void main(String[] args) {
7 Scanner sc = new Scanner(System.in);
8 System.out.print("Enter a sentence: ");
9 String s = sc.nextLine().toLowerCase();
10
11 Set<Character> set = new HashSet<>();
12 for (char c : s.toCharArray()) {
13 if (c >= 'a' && c <= 'z') {
14 set.add(c);
15 }
16 }
17
18 if (set.size() == 26) {
19 System.out.println("Pangram");
20 } else {
21 System.out.println("Not Pangram");
22 }
23 sc.close();
24 }
25}

Output

Enter a sentence: The quick brown fox jumps over the lazy dog
Pangram

What's going on

We collect all distinct letters and check if we have 26 of them.