Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Java

โปรแกรม Java ตรวจสอบว่าเป็น Pangram หรือไม่


บทนำ

ในบทความนี้ เราจะเรียนรู้วิธีการตรวจสอบว่าสตริงที่กำหนดเป็น Pangram หรือไม่ สตริงเป็น Pangram หากประกอบด้วยตัวอักษรทุกตัวของอักษรอังกฤษ (A-Z) ไม่สนใจพิมพ์ใหญ่/เล็ก

ตัวอย่างการทำงาน

อินพุทตัวอย่าง:

Input string: Abcdefghijklmnopqrstuvwxyz

เอาต์พุทที่คาดหวัง:

Yes, the string is a pangram

อัลกอริทึม

ขั้นตอนที่ 1 - เริ่มต้น
ขั้นตอนที่ 2 - ประกาศสตริง input_string
ขั้นตอนที่ 3 - กำหนดค่า
ขั้นตอนที่ 4 - แปลงสตริงเป็นอาร์เรย์ตัวอักษร
ขั้นตอนที่ 5 - วนลูปตรวจสอบว่ามีครบทุกตัวอักษร a-z โดยใช้ charAt(i) - 'a' เป็นดัชนี
ขั้นตอนที่ 6 - แสดงผลลัพธ์
ขั้นตอนที่ 7 - จบการทำงาน

ตัวอย่างที่ 1: เขียนในฟังก์ชัน main โดยตรง

public class Pangram {
    static int size = 26;
    static boolean isLetter(char ch) {
        if (!Character.isLetter(ch))
            return false;
        return true;
    }
    
    public static void main(String args[]) {
        String input_string = "Abcdefghijklmnopqrstuvwxyz";
        System.out.println("The string is defined as: " + input_string);
        int string_length = input_string.length();
        input_string = input_string.toLowerCase();
        boolean[] is_true = new boolean[size];
        for (int i = 0; i < string_length; i++) {
            if (isLetter(input_string.charAt(i))) {
                int letter = input_string.charAt(i) - 'a';
                is_true[letter] = true;
            }
        }
        boolean result = true;
        for (int i = 0; i < size; i++) {
            if (!is_true[i])
                result = false;
        }
        if (result)
            System.out.println("\nYes, the string is a pangram");
        else
            System.out.println("\nNo, the string is not a pangram");
    }
}

เอาต์พุท

The string is defined as: Abcdefghijklmnopqrstuvwxyz
Yes, the string is a pangram

ตัวอย่างที่ 2: ใช้เมธอดแยก (Object-Oriented Style)

public class Pangram {
    static int size = 26;
    static boolean isLetter(char ch) {
        if (!Character.isLetter(ch))
            return false;
        return true;
    }
    
    static boolean check_alphabets(String input_string, int string_length) {
        input_string = input_string.toLowerCase();
        boolean[] is_true = new boolean[size];
        for (int i = 0; i < string_length; i++) {
            if (isLetter(input_string.charAt(i))) {
                int letter = input_string.charAt(i) - 'a';
                is_true[letter] = true;
            }
        }
        for (int i = 0; i < size; i++) {
            if (!is_true[i])
                return false;
        }
        return true;
    }
    
    public static void main(String args[]) {
        String input_string = "Abcdefghijklmnopqrstuvwxyz";
        System.out.println("The string is defined as: " + input_string);
        int string_length = input_string.length();
        if (check_alphabets(input_string, string_length))
            System.out.println("\nYes, the string is a pangram");
        else
            System.out.println("\nNo, the string is not a pangram");
    }
}

เอาต์พุท

The string is defined as: Abcdefghijklmnopqrstuvwxyz
Yes, the string is a pangram

Pangram ยอดนิยม (The quick brown fox jumps over the lazy dog):

String pangram = "The quick brown fox jumps over the lazy dog";
// มีตัวอักษรครบ 26 ตัว → เป็น Pangram

วิธีอื่นๆ (Java 8+):

// ใช้ Stream API
public static boolean isPangram(String s) {
    return s.toLowerCase().chars()
        .filter(Character::isLetter)
        .distinct()
        .count() == 26;
}

// ใช้ BitSet (ประหยัดหน่วยความจำ)
public static boolean isPangramBitSet(String s) {
    BitSet bits = new BitSet(26);
    for (char c : s.toLowerCase().toCharArray()) {
        if (c >= 'a' && c <= 'z') bits.set(c - 'a');
    }
    return bits.cardinality() == 26;
}