ในบทความนี้ เราจะเข้าใจวิธีการตรวจสอบว่า string ที่กำหนดเป็น pangram ได้อย่างไร สตริงคือสตริง pangram หากมีอักขระทั้งหมดของตัวอักษรโดยไม่คำนึงถึงตัวพิมพ์ของตัวอักษร
ด้านล่างนี้เป็นการสาธิตสิ่งเดียวกัน -
สมมติว่าข้อมูลที่เราป้อนคือ −
Input string: Abcdefghijklmnopqrstuvwxyz
ผลลัพธ์ที่ต้องการจะเป็น −
Yes, the string is a pangram
อัลกอริทึม
Step 1 - START Step 2 - Declare a string value namely input_string. Step 3 - Define the values. Step 4 - Convert the input string to a character array. Step 5 - Iterate over the character of the array and check if the array contains all the alphabets using charAt(i) - 'a'. If yes, it’s a Pangram string. Step 6 - Display the result Step 7 - Stop
ตัวอย่างที่ 1
ที่นี่ เราเชื่อมโยงการดำเนินการทั้งหมดเข้าด้วยกันภายใต้ฟังก์ชัน 'หลัก'
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;
for (int i = 0; i < size; i++) {
if (!is_true[i])
result = false;
}
result = true;
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
ในที่นี้ เราสรุปการดำเนินการเป็นฟังก์ชันที่แสดงการเขียนโปรแกรมเชิงวัตถุ
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