Java program to check whether the String is Pangram or Not

Java program to check whether the String is Pangram or Not

Java program to check whether the String is Pangram or Not:

What is Pangram ?

If a given sentence has all the alphabets then it is a valid pangram.

 

If the below sentence is given as input then it has to return “Pangram”
The quick brown fox jumps over the lazy dog

 

If the below sentence is given as input then it has to return “Not a Pangram”
The quick brown fox jumps over the dog

Pseudo code or Logic to check whether the string is pangram or not:

  1. Use Scanner nextLine() method to get the complete entered line as one whole string and convert all to lowercase using toLowerCase() method.
  2. Use getBytes() method to convert all the alpha letters to byte code values, which return byte[].
  3. Create a arraylist with type byte/integer and iterate byte[] array which we got in the above step, iterate and move everything to this arraylist. We are doing this, because we wanted to use contains function, which has only in the arraylist and not in the byte array (even Byte wrapper class).
  4. As ASCII value starts from 97 to 122 for all the small alphabets, we performed toLowerCase() method in first step and kept the for loop iteration from 97 to 122.
  5. Just to cross check all the characters are exist we created the temp variable and checking whether its count is 26 or not before confirming whether it is a pangram or not.

 

Java Program to check whether the string is pangram or not:

package in.javadomain;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class AllLettersContains {

public static void main(String[] args) {
System.out.println("Enter the sentence to check whether it is a pangram or not ?");
Scanner sc = new Scanner(System.in);
String receivedSentence = sc.nextLine();
System.out.println(isPangramOrNot(receivedSentence));
}

static String isPangramOrNot(String textEntered) {
// converted everything to lower case.
byte[] byteValuesReceivedSentence = textEntered.toLowerCase().getBytes();

List<Integer> alphaBytesList = new ArrayList<Integer>();
for (int i = 0; i < byteValuesReceivedSentence.length; i++) {
alphaBytesList.add(((Byte) byteValuesReceivedSentence[i]).intValue());
}

int tempMove = 0;
// only for small letters
// start from 65 if you need to include capital letters as well.
// but tempMove = 26 need to be rewritten to handle appropriately.
for (int i = 97; i <= 123; i++) {
if (alphaBytesList.contains(i)) {
tempMove++;
}
}

if (tempMove == 26) {
return "pangram";
} else {
return "not a pangram";
}
}
}

 

 

Output:

Pangram:

Enter the sentence to check whether it is a pangram or not ?
The quick brown fox jumps over the lazy dog
pangram

 

Not a Pangram:

Enter the sentence to check whether it is a pangram or not ?
The quick brown fox jumps over the dog
not a pangram

 

694 total views, 1 views today

Leave a Reply

Your email address will not be published. Required fields are marked *