CC-6 : Check if String is a pangram.
Description:
A String is a pangram if it contains all characters of the alphabet. Given input String str, if str contains all letters of the alphabet, return true, else return false.
Test cases and expected outputs:
| Input Parameters | Expected outputs |
|---|---|
| str="The quick brown fox jumps over the lazy dog" | The input string is a pangram. |
| str="A quick movement of the enemy will jeopardize six gunboats" | The input string is a pangram. |
| str="A few words are better than none"; | The input string is *not* a pangram. |
Pseudocode:
| The java method should accept following input parameters: str (String). |
| Convert characters within str to lowercase using String.toLowerCase() method. |
| Extract the characters from str into char array names chars. |
| Initialize char array abt with individual characters from the string “abcdefghijklmnopqrstuvwxyz”. |
| Initialize boolean variable aFound to false. We will use aFound to track if a particular char within str exists within abt or not. |
| Iterate through chars using for loop using aidx as loop variable, starting from index 0 and incrementing aidx till we reach chars.length-1:
Set aFound to false.
Iterate through abt using for loop using cidx as loop variable, starting from index 0 and incrementing cidx till we reach abt.length-1:
If abt[aidx]==abt[cidx] then current character of abt exists within chars.
If the above inner loop has completed and aFound is still false, it means the current character of abt does not exist in the input string, so the input string is not a pangram. Return false from the program.
|
| If above outer loop has completed, it means input String contains all characters of the alphabet. So, it is a pangram, so return true from the program. |
The above algorithm can also be completed using Java String APIs :
Pseudocode:
| Initialize char array abt with individual characters from the string “abcdefghijklmnopqrstuvwxyz”. |
| Initialize boolean variable aFound to false. We will use aFound to track if a particular char within str exists within abt or not. |
| Iterate through chars using for loop using aidx as loop variable, starting from index 0 and incrementing aidx till we reach chars.length-1:
Check if current character of abt does not exist within str using String.contains() method.
If the current character does not exist within str, the input string is not a pangram. Return false from the program.
|
| If above outer loop has completed, it means input String contains all characters of the alphabet. So, it is a pangram, so return true from the program. |
Code:
public boolean stringCheckAlphabet(String str) {
char[] chars=str.toLowerCase().toCharArray();
char[] abt="abcdefghijklmnopqrstuvwxyz".toCharArray();
boolean aFound=false;
for (int aidx=0; aidx < abt.length; aidx++) {
aFound=false;
for (int cidx=0; cidx < chars.length; cidx++) {
if (abt[aidx]==chars[cidx]) {
aFound=true;
}
}
if (aFound==false) {
return false;
}
}
return true;
}
public boolean stringCheckAlphabetWithAPI(String str) {
String abt="abcdefghijklmnopqrstuvwxyz";
boolean aFound=false;
for (int aidx=0; aidx < abt.length(); aidx++) {
if (str.contains(abt.subSequence(aidx, aidx+1))==false) {
return false;
}
}
return true;
}
Click here to download and run code and test cases !
| About Us | Privacy Policy | Contact us |