Computer >> Computer tutorials >  >> Programming >> Java

Java Program to Check Whether the Given String is Pangram


In this article, we will understand how to check whether the given string is pangram. A string is a pangram string if it contains all the character of the alphabets ignoring the case of the alphabets.

Below is a demonstration of the same −

Suppose our input is

Input string: Abcdefghijklmnopqrstuvwxyz

The desired output would be

Yes, the string is a pangram

Algorithm

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

Example 1

Here, we bind all the operations together under the ‘main’ function.

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

Output

The string is defined as: Abcdefghijklmnopqrstuvwxyz

Yes, the string is a pangram

Example 2

Here, we encapsulate the operations into functions exhibiting object-oriented programming.

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

Output

The string is defined as: Abcdefghijklmnopqrstuvwxyz

Yes, the string is a pangram