javastringsethashsetpangram

Pangram using hashset in java


i am trying to determine whether the string is pangram or not by using set in Java

I've tried the below code.Now the output is showing as not pangram but it should be a pangram. Pls tell me whats wrong in my solution

    // Java Program to illustrate Pangram 
    import java.util.*;
    public class GFG 
    { 
        public static boolean checkPangram (String str) 
        { 
            int index = 0,count=0; 
            char s[]=str.toCharArray();
            Set<Character> hs= new HashSet<Character>();
            for(index=0;index<str.length();index++)
            {
                hs.add(s[index]); 
            }
            Iterator<Character> i=hs.iterator();
            while(i.hasNext())
            {
              count++;
              i.next();
            }
            if(count==26)
              return true;
            return false;
        } 

        // Driver Code 
        public static void main(String[] args) 
        { 
            String str = "the quick brown fox jumps over the lazy dog"; 

            if (checkPangram(str) == true) 
                System.out.print(str + " is a pangram."); 
            else
                System.out.print(str+ " is not a pangram."); 

        } 
    } 

output should be either true or false but I get no output


Solution

  • Iterator::hasNext checks if there is next element to iterate, but it is not moving to the next element. To move iterator to the next element you have to use Iterator::next which returns next element. Change your while loop to :

    while (i.hasNext()) {
        count++;
        i.next();
    }
    

    You have to remove spaces from your String before you convert it to char array as spaces should not be taken into consideration for pangrams. Also when creating your Set you should iterate until length of your char array is reached - not the length of input String (because we will remove spaces) :

    public static boolean checkPangram(String str) {
        int index = 0, count = 0;
    
        char s[] = str.replaceAll("\\s+","") //remove spaces
                .toCharArray();
    
        Set<Character> hs = new HashSet<Character>();
    
        for (index = 0; index < s.length; index++) { //iterate over your charArray
            hs.add(s[index]);
        }
    
        Iterator<Character> i = hs.iterator();
    
        while (i.hasNext()) {
            count++;
            i.next();
        }
    
        return count == 26;  //simplified condition result to be returned
    
    }
    

    However to be honest you do not need iterator at all. You can just check Set size :

    public static boolean checkPangram(String str) {
        char[] s = str.replaceAll("\\s+", "")
                    .toCharArray();
    
        Set<Character> hs = new HashSet<Character>();
    
        for (int index = 0; index < s.length; index++) {
            hs.add(s[index]);
        }
    
        return hs.size() == 26;
    }