Updated February 28, 2023
Definition
A pangram program is a computer program designed to check if a given sentence or phrase contains all the letters of the alphabet at least once. The purpose of this program is to determine if a given text is a pangram, which is a sentence that uses every letter of the alphabet at least once. You can write the program in various programming languages such as Python, C++, Java, etc.
Explanation of the Pangram Program in Java
This Pangram name comes from a Greek root which implies pan means ‘all’ and gram means ‘ something recorded or written.’ Let’s take a look at some pangram strings:
- Pack my box with five dozen liquor jugs
- My ex-pub quiz crowd gave joyful thanks
- Fix problems quickly with galvanized jets
You can write a pangram program in Java in various ways, but the basic idea remains the same. Below you can find the complete pangram program in Java for better understanding.
Code:
import java.util.Scanner;
import java.util.Set;
import java.util.stream.Collectors;
public class PangramChecker {
public static void main(String[] args) {
String alphabet = "abcdefghijklmnopqrstuvwxyz";
alphabet = alphabet.toLowerCase();
Scanner sc = new Scanner(System.in);
System.out.println("Enter a sentence: ");
String input = sc.nextLine();
input = input.toLowerCase();
input = input.replaceAll("[^a-z]+", "");
Set inputSet = input.chars().mapToObj(e -> (char) e).collect(Collectors.toSet());
Set alphabetSet = alphabet.chars().mapToObj(e -> (char) e).collect(Collectors.toSet());
if(alphabetSet.equals(inputSet)) {
System.out.println("The sentence is a pangram");
} else {
System.out.println("The sentence is not a pangram");
}
sc.close();
}
}
Output:
The output will change if any alphabet is missing.
Key Takeaway
- A pangram program checks if a given text contains all the letters of the alphabet.
- The program works by converting the input text and the alphabet to sets and then comparing the two sets to determine if they are equal.
- You can write the program in various programming languages, including Java, Python, C++, etc.
- You can use the program for educational purposes, to test your language skills, or to test the functionality of a keyboard or other input device.
Examples of Pangram Program in Java
In the following examples, we will create a Java program to check the given string is a pangram or not.
#1. By using a Frequency Array
A pangram program can also be written using a frequency array, which is an array that stores the frequency of each letter in the alphabet. In this program, each letter is either converted into upper or lowercase. In the next step, an array is created to note down the frequency of all alphabets from a to z. If in case any alphabet is missing, the output will be ‘The sentence is not a pangram’ else, the output will be ‘The sentence is a pangram’.
The following is a sample Java program that implements this approach:
Code:
import java.util.Scanner;
public class PangramChecker {
public static void main(String[] args) {
int[] freq = new int[26];
Scanner sc = new Scanner(System.in);
System.out.println("Enter a sentence: ");
String input = sc.nextLine();
input = input.toLowerCase();
for (int i = 0; i < input.length(); i++) { char c = input.charAt(i); if (c >= 'a' && c <= 'z') {
freq[c - 'a']++;
}
}
boolean isPangram = true;
for (int i = 0; i < 26; i++) {
if (freq[i] == 0) {
isPangram = false;
break;
}
}
if (isPangram) {
System.out.println("The sentence is a pangram");
} else {
System.out.println("The sentence is not a pangram");
}
sc.close();
}
}
Output
In this example, the input text is “The quick brown fox jumps over the lazy dog”, which is a pangram. The program correctly identifies the text as a pangram and outputs, “The sentence is a pangram”.
The output will change if any alphabet is missing. So the output will be “The sentence is not a pangram”.
#2. By Using Traversal
A pangram program can also be written using traversal, which is the process of visiting each letter of the alphabet one by one. In this approach, all letters will be converted into lowercase. The next step is to traverse all the characters right from a to z. If all the letters (a to z) are found, print ‘ The sentence is a pangram, else print ‘The sentence is not a pangram’
The following is a sample Java program that implements this approach:
Code:
import java.util.Scanner;
public class PangramChecker {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a sentence: ");
String input = sc.nextLine();
input = input.toLowerCase();
boolean[] alphabet = new boolean[26];
for (int i = 0; i < input.length(); i++) { char c = input.charAt(i); if (c >= 'a' && c <= 'z') {
alphabet[c - 'a'] = true;
}
}
boolean isPangram = true;
for (int i = 0; i < 26; i++) {
if (!alphabet[i]) {
isPangram = false;
break;
}
}
if (isPangram) {
System.out.println("The sentence is a pangram");
} else {
System.out.println("The sentence is not a pangram");
}
sc.close();
}
}
Output:
In this example, the input text is “The five boxing wizards jumps quickly” which is a pangram. The program correctly identifies the text as a pangram, and the output is, “The sentence is a pangram”.
The output will change if any alphabet is missing. So the output will be “The sentence is not a pangram”.
Conclusion
To conclude a pangram program, check if a given text is pangram by comparing it to the alphabet and checking if it contains all the letters. You can use this program for various purposes, such as education, testing, or entertainment. By understanding the basic concepts and algorithms behind a pangram program, one can develop a program that meets their specific needs and requirements.
Recommended Article
We hope that this EDUCBA information on “Pangram Program in Java” was beneficial to you. You can view EDUCBA’s recommended articles for more information,