![Trending Articles on Technical and Non Technical topics](/images/trending_categories.jpeg)
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java program to check if string is pangram
A pangram is a string that contains all the letters of the English alphabet. An example of a pangram is "The quick brown fox jumps over the lazy dog".
Problem Statement
Given a string, write a Java program to check whether it is pangram or not.
Consider the following example -
InputThe quick brown fox jumps over the lazy dog.Output
String: The quick brown fox jumps over the lazy dog. The above string is a pangram.
Algorithm
The algorithm below is focusing to check if the string is pangram.
Step 1: Create a boolean array `alphaList` of size 26 to track the presence of each letter.
Step 2: Initialize an integer `flag` to 1, assuming the string is a pangram.
Step 3: Check each character in the String: Loop through each character in the string.
Step 4: If the character is a letter (uppercase or lowercase), calculate its index in the alphabet and mark the corresponding position in `alphaList` as `true`.
Step 5: Verify all letters: Loop through `alphaList`
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`.
Java program to check if string is pangram
A program that checks if a string is 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.
Code Explanation
The string 'str' is traversed using a for loop and for every alphabet, the corresponding index in aplhaList is set to true.
The code snippet that demonstrates this is given as follows −
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; }
After this, the array alphaList is traversed. If all the values are true, then string is a pangram and value of flag remains 1. However, if even 1 value is false, then string is not a pangram and the value of flag is set to 0. Then it is displayed if string is pangram or not.
The code snippet that demonstrates this is given as follows.
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."); }