Check Whether a Given String is Pangram in Java



In this article, we will understand how to check whether the given string is a pangram in Java. A string is a pangram string if it contains all the characters of the alphabet ignoring the case of the alphabet. Here, we will be using two different approaches: using the main() method and using encapsulation.

Problem Statement

Given a string write a program in Java to find whether the string is pangram or not. Below is a demonstration of the same ?

Input 

Input string: Abcdefghijklmnopqrstuvwxyz

Output

Yes, the string is a pangram

Different approaches

Following are the steps to check whether the given string is pangram ?

Using main() method

Following are the steps to check whether the given string is pangram using the main() method ?

  • First, we will create a class Pangram with a constant size = 26 and a helper method isLetter(char ch) to check if a character is a letter.
  • In main(), define the input string "Abcdefghijklmnopqrstuvwxyz", print it, and convert it to lowercase.
  • Create a boolean array is_true of size 26 to mark each alphabet letter as found.
  • Loop through input_string using the for loop for each letter, and mark its position in is_true.
  • Loop through is_true and check will do the conditional if else statement if any entry is false, the result is false.
  • Print "Yes, the string is a pangram" if all letters are present; otherwise, print "No, the string is not a pangram."

Example

Here, we bind all the operations together under the main() method ?

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

Using encapsulation

The following are the steps to check whether the given string is pangram using encapsulation ?

  • First, we will define Pangram with size = 26 and a helper method isLetter(char ch).
  • Create check_alphabets(String input_string, int string_length) to convert the input to lowercase, mark letters in a boolean array, and verify if all letters are present.
  • In main(), initialize the input string "Abcdefghijklmnopqrstuvwxyz", print it, and call check_alphabets.
  • Based on the check_alphabets return value, print "Yes, the string is a pangram" or "No, the string is not a pangram."

Example

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
Updated on: 2024-11-05T22:03:46+05:30

621 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements