Home > JAVA > Pangrams

Pangrams

Problem:

Roy wanted to increase his typing speed for programming contests. So, his friend advised him to type the sentence “The quick brown fox jumps over the lazy dog” repeatedly because it is a pangram. ( pangrams are sentences constructed by using every letter of the alphabet at least once. ) After typing the sentence several times, Roy became bored with it. So he started to look for other pangrams. Given a sentence s, tell Roy if it is a pangram or not.

From Wikipedia, A pangram or holoalphabetic sentence for a given alphabet is a sentence using every letter of the alphabet at least once. Check both uppercase and lowercase(+32).

Using Java String function indexOf() to check if the character appear in the sentence.

Java code:


import java.util.Scanner;

public class Pangram {

public static void main(String[] args) {
 Scanner in = new Scanner(System.in);
 String input = in.nextLine();
 if(testPangram(input)) {
 System.out.println("pangram");
 } else {
 System.out.println("not pangram");
 }
 in.close();
 }

 static boolean testPangram(String input) {
 for(char a = 'A' ; a <= 'Z' ; a++) {
 if(input.indexOf(a) < 0 && input.indexOf((char) a+32) < 0) {
 return false;
 }
 }
 return true;
 }
}

Result for input “We promptly judged antique ivory buckles for the prize “:

pangrams2

Result for input “ABCDEFGHIJKLMNOPQSTUVWXYZ”:

pangrams1

Source:

  1. https://www.hackerrank.com/challenges/pangrams
  2. http://en.wikipedia.org/wiki/Pangram
Categories: JAVA Tags: , ,
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: