Computer >> Máy Tính >  >> Lập trình >> Java

Chương trình Java để kiểm tra xem chuỗi đã cho có phải là Pangram hay không

Trong bài này, chúng ta sẽ hiểu cách kiểm tra xem chuỗi đã cho có phải là pangram hay không. Một chuỗi là một chuỗi pangram nếu nó chứa tất cả các ký tự của bảng chữ cái bỏ qua trường hợp của bảng chữ cái.

Dưới đây là một minh chứng về điều tương tự -

Giả sử đầu vào của chúng tôi là -

Input string: Abcdefghijklmnopqrstuvwxyz

Đầu ra mong muốn sẽ là -

Yes, the string is a pangram

Thuật toán

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

Ví dụ 1

Ở đây, chúng tôi liên kết tất cả các hoạt động với nhau trong hàm "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;
      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");
   }
}

Đầu ra

The string is defined as: Abcdefghijklmnopqrstuvwxyz

Yes, the string is a pangram

Ví dụ 2

Ở đây, chúng tôi đóng gói các hoạt động thành các hàm thể hiện lập trình hướng đối tượng.

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");
   }
}

Đầu ra

The string is defined as: Abcdefghijklmnopqrstuvwxyz

Yes, the string is a pangram