Thanks to visit codestin.com
Credit goes to www.tutorialspoint.com

Check if String is Pangram in Java



The given task is to write a Java program to check whether a string is a pangram or not. A string is called a pangram if and only if it contains all the letters of the English alphabet, regardless of their case.

Example Scenario:

Let's understand the problem with an example -

Input: "The quick brown fox jumps over the lazy dog"
Output: Yes, the string is a pangram 

Read the input String, you can find all the letters of the English alphabet in it. Therefore, it is a pangram string.

How to Check if a String is a Pangram

Follow the steps given below to check if the string is a pangram:

  • Step 1: Create a boolean array of size 26 to track the presence of each letter.

  • Step 2: We first assume the given string is a pangram. For this, initialize an integer flag to 1.

  • Step 3: Iterate through each character in the String using Loop.

  • Step 4: If the character is a letter (uppercase or lowercase), calculate its index in the alphabet and mark the corresponding position in array as "TRUE".

  • Step 5: Loop through the array.

  • Step 6: If any position is "FALSE", set flag to 0 (indicating the string is not a pangram).

  • Step 7: Print the original string and whether it is a pangram based on the value of flag variable.

Program 1

A program that checks if a string is a pangram or not is given as follows:

public class Example {
   public static void main(String[] args) {
      String str = "The quick brown fox jumps over the lazy dog";
      boolean[] alphaList = new boolean[26];
      int index = 0;
      int flag = 1;
      for (int i = 0; i < str.length(); i++) {
         if ( str.charAt(i) >= 'A' && str.charAt(i) <= 'Z') {
            index = str.charAt(i) - 'A';
         } else if( str.charAt(i) >= 'a' &&  str.charAt(i) <= 'z') {
            index = str.charAt(i) - 'a';
         }
         alphaList[index] = true;
      }
      for (int i = 0; i <= 25; i++) {
         if (alphaList[i] == false)
            flag = 0;
      }
      System.out.print("String: " + str);
      if (flag == 1)
         System.out.print("\nThe above string is a pangram.");
      else
         System.out.print("\nThe above string is not a pangram.");
   }
}

Output

String: The quick brown fox jumps over the lazy dog
The above string is a pangram.

Program 2

This is another Java program that checks whether the string is a pangram. Here, we encapsulate the operations in functions.

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("Yes, the string is a pangram");
   else
      System.out.println("No, the string is not a pangram");
   }
}

Output

The string is defined as: Abcdefghijklmnopqrstuvwxyz
Yes, the string is a pangram
Updated on: 2025-04-18T18:49:33+05:30

11K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements