×

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

How to create an array in Java

An array is a linear data structure stores a collection of fixed number ofelements of similar type. The size of an array is specified when it is created. The array...

14 minutes read.

How many ways to create object in Java?

In this article, you will be acknowledged about the different ways to create an object in java. So far you construct an object from a class, as is common knowledge,...

6 minutes read.

Java Wrapper classes

Java Wrapper classes A wrapper class is a class whose object contains a primitive data type; moreover it provides a way to use primitive data type (int, boolean, etc.) as objects. Wrapper...

2 minutes read.

Java Math addExact() Method

The addExact() method of Math class returns the sum of the two arguments, throwing an exception if the result overflows a long or an int. Syntax public static int addExact (int x,...

1 minute read.

Heart Pattern in Java

Heart Pattern is yet another intricate pattern program, however, due to its complexity, interviewers hardly ever inquire about it. Method for Printing the Heart Number Pattern Put the value of the total row...

2 minutes read.

Magic Square in Java

A square with numbers is referred to as a magic square. The numbers in a magic square of order n are arranged to ensure that the sum of all of...

4 minutes read.

Java gc()

Garbage collection Java language provides different ways to perform the task of memory management. In Java, objects are declared and assigned references. Once lifeof an object is completed it is dereferenced...

4 minutes read.

How to check if date is valid in Java?

In this article, you will acknowledge about how to verify if a date is valid or not. For this you will learn the approach, you will be able to write...

3 minutes read.

Java finalize()

Java finalize() In Java, the Object class is the root class that is inherited by all the Java classes. The class provides the finalize() method that is called just before the...

4 minutes read.

Swapping Program in Java

Swapping Program in Java The swapping program in Java is used to interchange the values of the two variables. For example, if X = 12 and Y = 24, then the...

4 minutes read.

How to Convert long to String in Java

How to Convert long to String in java It is used if we have to display a long number in the text field because everything is displayed as a String. A...

3 minutes read.

Java Program to find the smallest element in a tree

The variable min is used to store the data of the root, which is initially defined. The smallest node in the left subtree is then located by moving through the...

3 minutes read.

How to Calculate Week Number From Current Date in Java?

The WeekFields class's weekOfMonth() method is utilized to return the field for access the week of a month based on this WeekFields. If the first day of the month is a...

3 minutes read.

Java Switch Keyword

In this article we are going to learn the concept of a java switch keyword. Generally, java case keyword is used with the switch statements or keyword.Switch keyword is implemented in...

3 minutes read.

What’s new in Java 12

On March 19th, 2019, the Java 12th edition was released. After releasing this edition, they have decided to release every new edition every six months. This version is the advanced...

5 minutes read.

Prepared statement in Java

Prepared statement: A prepared statement is a statement that is pre-compiled SQL statement, and it is a sub-interface of a statement. Compared to other statement objects in java, Prepared Statement objects have...

3 minutes read.

Java Math log1p() Method

The log1p() method of Math class returns the natural logarithmic sum for the specified double argument and 1. Its value is much closer to result of ln(1 + x). Syntax: public static...

2 minutes read.

Array and String with Examples in Java

Array in Java: An array in Java is a group of variables with similar types that have a common name. The arrays used in Java differ from those used in C/C++. Key...

6 minutes read.

Hashing Algorithm in Java

The hashing algorithm is a method that maps data to the fixed-length hash. The Java hash-based algorithm employs a cryptographic mathematical operation. A hash technique or hash function is supposed...

8 minutes read.

Difference Between replace() and replaceall() in Java

In this article, you will be acknowledged about the replace() and replaceall() methods in java. Also, most importantly you will be acknowledged the differences between them along with the example...

4 minutes read.