This question can be asked in different ways like " tell if a String contains a-z character or not" and "How can we check a String contains only Alphabet".
To solve a String contains all the alphabet or not, we will take set. We will Iterate the String and every time we will add the iterated character into the Set.
If the set size is 26 that means the String contains all the alphabet.
Here is the code snippet:
public class AllAlphabetInString {
public static void main(String[] args) {
int noOfAlphabet=26;
String text="We promptly judged antique ivory buckles for the next prize";
Set<String> alphaSet=new HashSet<>();
//uses regular expression to filter out special character from the String..
Pattern pattern=Pattern.compile("[A-Za-z]");
Matcher matcher=pattern.matcher(text);
boolean findAll=false;
while(matcher.find()&&!findAll)
{
String s=matcher.group().toLowerCase();
alphaSet.add(s);
if(alphaSet.size()==noOfAlphabet)
{
findAll=true;
}
}
System.out.println("Result: "+findAll);
}
}
To solve a String contains all the alphabet or not, we will take set. We will Iterate the String and every time we will add the iterated character into the Set.
If the set size is 26 that means the String contains all the alphabet.
Here is the code snippet:
public class AllAlphabetInString {
public static void main(String[] args) {
int noOfAlphabet=26;
String text="We promptly judged antique ivory buckles for the next prize";
Set<String> alphaSet=new HashSet<>();
//uses regular expression to filter out special character from the String..
Pattern pattern=Pattern.compile("[A-Za-z]");
Matcher matcher=pattern.matcher(text);
boolean findAll=false;
while(matcher.find()&&!findAll)
{
String s=matcher.group().toLowerCase();
alphaSet.add(s);
if(alphaSet.size()==noOfAlphabet)
{
findAll=true;
}
}
System.out.println("Result: "+findAll);
}
}
No comments:
Post a Comment