×

Pangram Program in Java

If a string comprises all alphabet letters from A to Z or from a to z without regard to case, it is referred to as a pangram.

Some examples of pangram strings or pangram sentences are:

Pangram strings include the following phrases:

  • The five boxing wizards jump quickly
  • The quick brown fox jumps over the lazy dog
  • My ex-pub quiz crowd gave joyful thanks.
  • Pack my box with five dozen liquor jugs.
  • Fix problem quickly with galvanized jets.

Pangram Algorithm

  • Build a hash table (boolean vector). To indicate which characters in the string are present.
  • Traverse the entire string of characters that has been provided.
  • Subtract "A" from the uppercase letter if it is located to obtain the index.
  • If the lowercase letter is discovered, take "a" off to discover the index.
  • Mark the vector's value as true (character found).
  • Return false if any character is not marked (a character not found).
  • If not, return true.

Java program for pangram

Each of the following two methods for locating the pangram string:

  1. With frequency array
  2. With Traversal

With Frequency Array

  1. Each letter should be changed to uppercase or lowercase.
  2. Make an array to record the frequency of each letter of the given text (from a to z).
  3. Traverse the frequency spectrum. Print "the string isn't a pangram string" if an alphabet is missing from the frequency array; else, print "the phrase is a pangram string".

Let's include the strategy mentioned above in a Java program.

Pangram.java

public class Pangram 
{  
static int S = 26;  
// function to determine whether or not a character is a letter
static boolean ifalp(char ch)  
{  
if (!Character.ifalp(ch))  
// if the character is not a letter, it returns false.
return false;  
// if the character is a letter, it returns true.   
return true;  
}  
// function to determine whether the input string contains all capital letters (a to z).  
static boolean letter(String str, int l)  
{  
// it converts to lowercase when a string is provided.
str = str.toLowerCase();  
// making a boolean array that contains the letters that are present  
boolean[] flag = new boolean[S];  
// loop over the string's characters one by one  
for (int i = 0; i < l; i++)   
{  
// determines whether the current character is a letter.  
if (ifalp(str.charAt(i)))   
{  
int letter = str.charAt(i) - 'a';  
// the current letter is deemed to be present. 
flag[letter] = true;  
}  
}  
// cycle through each letter in the string provided in a loop.  
for (int i = 0; i < S; i++)   
{  
if (!flag[i])  
// if the present letter is not found in the string, it returns false.  
return false;  
}  
// returns true if the string contains the current character.  
return true;  
}  
 
public static void main(String args[])  
{    
String str = " The five boxing wizards jump quickly";    
int l = str.length();   
if (letter(str, l))  
System.out.println("The string is a pangram ");  
else  
System.out.println("The string is not a pangram ");  
}  
}  

Output

The string is a pangram.

With Traversal

In this method, all the letters are first changed to lowercase. Then, go over each character, starting with a and ending with z. Check to see if the given string contains every letter (a to z); if so, print the pangram; otherwise, print the non-pangram.

Let's include the strategy mentioned above in a Java programme.

Pangram1.java

public class Pangram1 
{  
// function to determine whether the input string contains all capital letters (a to z).    
public static void containsall(String str)  
{  
// it converts to lowercase when a string is provided
str = str.toLowerCase();  
boolean all = true;  
// loop over the string's characters one by one  
for (char ch = 'a'; ch <= 'z'; ch++)   
{  
// determines whether the current character is a letter.  
if (!str.contains(String.valueOf(ch)))   
{  
all = false;  
break;  
}  
}  
// determines whether or not all of the letters are present.
if (all)  
 
System.out.println("the present string is Pangram String");  
else  


System.out.println("the present string is Not a Pangram String");  
}  
  
public static void main(String args[])  
{  
String string = " The five boxing wizards jump quickly";  
containsall(string);  
}  
}  

Output

the present string is Pangram String

Time Complexity: The string's length, n, determine the time complexity of the problem, which is O(n).

Space Complexity: It has an O(1) space complexity since no additional space is required.


Related Topics

Java Short Keyword

Java supports eight different primitive datatypes. The language has predefined primitive datatypes that are given keyword names. Let's take a closer look at each of the eight primitive data types....

3 minutes read.

Java String replaceAll() method

Java String replaceAll() method returns a String replacing all the sequence of characters matching regular expression i.e regex and replacement string. Syntax: public String replaceAll(String regex, String replacement) Parameters: regex : regular expression replacement :...

1 minute read.

Java vs Scala

Java : Java is an object oriented programming language. It is also known as multi threaded language. It was designed by James gosling in the year 1995. We can also say...

4 minutes read.

Kong Java Client

Kong is an Organization Microservice Programming interface gateway. Kong gives an adaptable deliberation layer that safely oversees correspondence among clients and microservices by means of a Programming interface. Otherwise called...

6 minutes read.

Java Garbage Collection

Java Garbage Collection In Java, unreferenced objects are treated like garbage. The process of reclaiming the unused memory during runtime automatically is known as the Java Garbage Collection.In other words, the...

4 minutes read.

How to set timer in Java

In this article, you will be very well equipped with the knowledge to set timer in java. The timer in java can be set by using timer class provided by...

3 minutes read.

Java Math cosh() Method

The cosh() method of Math class returns the first hyperbolic cosine((e+e)/2) of a double value. Syntax: public static double cosh(double x) Parameters: The parameter ‘x’ represents the number whose hyperbolic cosine is to be...

2 minutes read.

How to get ASCII value of char in Java

Introduction: On this application, you will learn how to find and show the ASCII value of char in Java. That is done with the use of type-casting and also everyday...

4 minutes read.

Java Binary Tree

The non-linear data structure known as a binary tree is a type of tree, and because it stores data in a hierarchical manner, it is mostly utilised for finding and...

7 minutes read.

Why String in Immutable in Java?

Why String in Immutable in Java Immutable means unchangeable or unmodifiable.  Strings in Java are immutable, it means once a string is created, it cannot be modified or changed. Any change...

2 minutes read.

Java ArraylistRemove() Time Complexity

In this tutorial, we will learn about how we can remove time complexity in Java ArrayList. But unless we will not learn what is ArrayList, we don’t understand this process....

7 minutes read.

Java Editors

A straightforward text editor may be used to create Java applications. However, a Java integrated programming environment (IDE) enables the software developer to create programs more quickly. An IDE offers...

4 minutes read.

Java.net.ConnectionException

java.net.ConnectException: Connection rejected: the interface is the most continuous sort of happening, organizing special cases in Java at whatever point the product is in client-server engineering and attempting to make...

3 minutes read.

Java Linters

When it comes to programming, everyone makes mistakes. Errors are bad for developers since they are difficult to handle. But handling as many as possible errors will bring out the...

6 minutes read.

Computing Digit Sum of all Numbers from 1 to n in Java

In this tutorial, we will discuss various methods to compute the sum of digits of all numbers beginning from 1 to n through a Java program. We will achieve it through...

7 minutes read.

Java Try Keyword

The try block in Java is used to run essential code, such as connection closure, among other things. Whether an exception is resolved or not, the Java try block has...

3 minutes read.

Stack Program in Java

Stack Program in Java The Stack class is part of the collection framework that inherits the Vector class. Thus, the Stack class can also be called a subclass of the Vector...

5 minutes read.

Add Time in Java

Before Java 8, java.util.Date was one of the most usually involved classes for addressing date-time values in java. Then Java 8 presented java.time.LocalDateTime and java.time.ZonedDateTime. Java 8 likewise permits us...

6 minutes read.

Sliding Window Problem in Java

A sliding window is used in computer science and data science to process large datasets. It involves breaking the dataset into smaller chunks or windows and then processing it in...

6 minutes read.

What are Array strings in Java?

In normal programming, An array is a group and a collection of identical forms of data that are stored in a sequential memory region and may be accessed using their...

4 minutes read.