How to resolve the algorithm Pangram checker step by step in the Java programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Pangram checker step by step in the Java programming language
Table of Contents
Problem Statement
A pangram is a sentence that contains all the letters of the English alphabet at least once. For example: The quick brown fox jumps over the lazy dog.
Write a function or method to check a sentence to see if it is a pangram (or not) and show its use.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Pangram checker step by step in the Java programming language
Purpose: The program checks whether a given string is a pangram, i.e., contains every letter of the alphabet.
Implementation:
-
isPangram(String)
Method:- Iterates over lowercase and uppercase letters from 'A' to 'Z':
- If a character is not present (i.e.,
indexOf()
returns -1), it returnsfalse
.
- If a character is not present (i.e.,
- Returns
true
if all letters are present.
- Iterates over lowercase and uppercase letters from 'A' to 'Z':
-
main
Method:- Tests several input strings using
isPangram()
and prints the results:- "the quick brown fox jumps over the lazy dog": True
- "the quick brown fox jumped over the lazy dog": False (missing "s")
- "ABCDEFGHIJKLMNOPQRSTUVWXYZ": True
- "ABCDEFGHIJKLMNOPQSTUVWXYZ": False (missing "r")
- "ABCDEFGHIJKL.NOPQRSTUVWXYZ": False (missing "m")
- "ABC.D.E.FGHIJ/KL-M+NOPQ R\nSTUVWXYZ": True (ignores non-alphabetic characters)
- "": False (empty string)
- Tests several input strings using
Source code in the java programming language
public class Pangram {
public static boolean isPangram(String test){
for (char a = 'A'; a <= 'Z'; a++)
if ((test.indexOf(a) < 0) && (test.indexOf((char)(a + 32)) < 0))
return false;
return true;
}
public static void main(String[] args){
System.out.println(isPangram("the quick brown fox jumps over the lazy dog"));//true
System.out.println(isPangram("the quick brown fox jumped over the lazy dog"));//false, no s
System.out.println(isPangram("ABCDEFGHIJKLMNOPQRSTUVWXYZ"));//true
System.out.println(isPangram("ABCDEFGHIJKLMNOPQSTUVWXYZ"));//false, no r
System.out.println(isPangram("ABCDEFGHIJKL.NOPQRSTUVWXYZ"));//false, no m
System.out.println(isPangram("ABC.D.E.FGHI*J/KL-M+NO*PQ R\nSTUVWXYZ"));//true
System.out.println(isPangram(""));//false
}
}
You may also check:How to resolve the algorithm Empty program step by step in the SimpleCode programming language
You may also check:How to resolve the algorithm O'Halloran numbers step by step in the ALGOL 68 programming language
You may also check:How to resolve the algorithm Sorting algorithms/Bogosort step by step in the SNOBOL4 programming language
You may also check:How to resolve the algorithm Case-sensitivity of identifiers step by step in the Ada programming language
You may also check:How to resolve the algorithm Roman numerals/Decode step by step in the Cowgol programming language