-4

This the code I have been using but it is working for strings which are not pangram but not the other way. Also for the strings which have duplicate.

int i;
char l,ch;
String s="";
Scanner sc= new Scanner(System.in);
s=sc.next();
s=s.trim();
int c=0;
s=s.toLowerCase();
for (l='a';l<='z';l++) {
  for(i=0;i<s.length();i++) {
    ch=s.charAt(i);
    if(ch==l) {
      c++;
    }
  }
  if(c==0)
    break;
  else {
    c=0;
    continue;
  }
}
if(l=='z')
  System.out.println("pangram");
else
  System.out.println("not pangram");
Sani Huttunen
  • 23,620
  • 6
  • 72
  • 79

1 Answers1

0

You're checking if l is equal to z after the loop. If every letter except z is present in the sentence then you'd get a false positive since l will equal z.

Here you increment l for each loop:

for (l='a'; l<='z'; l++)

After the last loop (when l equals z) l will be incremented one more time.

What you need to do is to check if l is greater than z.

if (l > 'z')
  System.out.println("pangram");
...

Edit

sc.next() will return a string to the next token. (Line ending or whitespace)
If you input We promptly judged antique ivory buckles for the next prize s will only contain We since a space will be regarded as an "ending token" for next(). You need to use sc.nextLine() instead.

Sani Huttunen
  • 23,620
  • 6
  • 72
  • 79
  • it is still not working. My test case is "We promptly judged antique ivory buckles for the next prize".This is a pangram string but my program is showing not pangram. – shivansh mishra Aug 28 '16 at 20:52
  • That's probably because you are using `sc.next()` instead of `sc.nextLine()`. `next()` will return a string to the next token which in this case is a whitespace character. So `s` will equal `We` and nothing else. Use `nextLine()` instead. – Sani Huttunen Aug 28 '16 at 21:14