1

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

5 Answers5

2

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;
}
Michał Krzywański
  • 15,659
  • 4
  • 36
  • 63
  • Note that `replaceAll("\\s+", "")` only eliminates spaces, but a sentence will often contain punctuation too, e.g. a terminating period, so you might want to use one of these regex instead: `"[^a-zA-Z]+"` *(not character in range A-Z)*, `"\\P{Alpha}+"` *(not POSIX "alphabetic character")*, or `"\\P{L}+"` *(not unicode category "Letter")*. Note that the unicode variant will keep accented letters, e.g. `ó`, and that's a whole different set of problems for pangram checking. – Andreas Jul 20 '19 at 17:14
2

You need to learn how to debug your own code.
See What is a debugger and how can it help me diagnose problems?

Why is it returning false?
Because count is 27.

Why is count = 27?
Because you count the spaces too.

How do I fix that?
Call Character.isLetter(s[index]) to check before adding to hs.
See javadoc of Character: https://docs.oracle.com/javase/8/docs/api/java/lang/Character.html

Also notice that you don't want to count uppercase vs lowercase letters as being different, so you should call e.g. toLowercase(), in one of two ways:

char s[]=str.toLowercase().toCharArray()

or:

hs.add(Character.toLowercase(s[index]));
Andreas
  • 154,647
  • 11
  • 152
  • 247
0

There are some errors in your code which need to be rectified.

str.toCharArray() will also put white spaces in char s[]. Hence count will be 27 including white space. Instead, you can check for whitespace before putting into HashSet. Also no need to use while loop as we can directly get HashSet size. But in your code block, you are using while loop with iterator, so i.hasNext() will always be true, so execution is going into an infinite loop. To avoid this you need to use i.next().

Take a look at below code, you will understand.

package problems;

import java.util.HashSet;
import java.util.Set;

public class StringCompareTo {

    public static boolean checkPangram(String str) {
        int index = 0;
        char s[] = str.toCharArray();
        Set<Character> hs = new HashSet<Character>();
        for (index = 0; index < str.length(); index++) {
            if(!Character.isWhitespace(s[index]))
            hs.add(s[index]);
        }
        if (hs.size() == 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.");

    }
}

Use of while loop with iterator should be :

Iterator<Character> i = hs.iterator();
        while(i.hasNext()){
        char temp = i.next();
          count++;
        }
0

I presume this was an exercise but your only stipulation was to not use set. You could also do it this way. Streams and lambdas are not really advanced concepts but simply additional features that have been around since Java 8.

       String str = "the quick brown fox jumps over the lazy dog";
       System.out.println("The string is " + (isPangram(str) ? ""
                : "not ") + "a pangram.");
       }
       public static boolean isPangram(String str) {
          return Arrays.stream(str.split("")).filter(
                a -> a.matches("[A-Za-z]")).distinct().count() == 26;

       }

It elminates all but upper and lower characters and then puts them in a stream and filters out duplicates. Then it counts them. If the count equals 26 is is a pangram.

WJS
  • 36,363
  • 4
  • 24
  • 39
0
public static void main(String[] args){

String pangramTxt="The quick brown fox jumps over the lazy dog";
checkPangram(pangramTxt);


}

public static void checkPangram(String rawTxt){
    HashSet<Character> set=new HashSet<>();
    //remove nonword characters eg space etc
    
    char[] charArr=rawTxt.replaceAll("\\W+","").toLowerCase().toCharArray();
    
    for(Character val: charArr){
        set.add(val);
    }
    
    //26 ... the alphabet
    if(set.size()==26){
     System.out.println("Text is pangram: ************");
    }

}
  • 1
    Hi and welcome to SO! Please take the [tour](https://stackoverflow.com/tour). Thanks for contributing an answer, but can you also add an explanation on how your code solves the problem? – Jeanne Dark Nov 25 '20 at 18:32