Question 5
Pangram Checking
Given a string check if it is Pangram or not. A pangram is a sentence containing every letter in the English Alphabet.
Examples : The quick brown fox jumps over the lazy dog ” is a Pangram [Contains all the characters from ‘a’ to ‘z’]
“The quick brown fox jumps over the dog” is not a Pangram [Doesn’t contains all the characters from ‘a’ to ‘z’, as ‘l’, ‘z’, ‘y’ are missing]
We create a mark[] array of Boolean type. We iterate through all the characters of our string and whenever we see a character we mark it. Lowercase and Uppercase are considered the same. So ‘A’ and ‘a’ are marked in index 0 and similarly ‘Z’ and ‘z’ are marked in index 25.
After iterating through all the characters we check whether all the characters are marked or not. If not then return false as this is not a pangram else return true.
Code for checking whether a string is Pangram or not
// C Program to check the given string is a pangram or not #include<stdio.h> int main() { char str[120]; int i,alphabet[26]={0},result=0; printf("Enter the String: "); scanf("%s",str); for(i=0;str[i]!='\0';i++) { if('a'<=str[i] && str[i]<='z') { result+=!alphabet[str[i]-'a']; alphabet[str[i]-'a']=1; } else if('A'<=str[i] && str[i]<='Z') { result+=!alphabet[str[i]-'A']; alphabet[str[i]-'A']=1; } } if(result==26) { printf("The String is a Pangram"); } else { printf("The String is not a Pangram"); } return 0; }
Output
Enter the String: qwertyiopasdfghjklzxcvbnm
The String is a Pangram
// Java Program to check whether a string is Panagram or not import java.util.*; class Main { // Returns true if the string is pangram else false public static boolean checkPangram (String str) { // Create a hash table to mark the characters present in the string // By default all the elements of mark would be false. boolean[]mark = new boolean[26]; int index = 0; // Traverse all characters for (int i = 0; i < str.length (); i++) { // If uppercase character, subtract 'A'to find index. if ('A' <= str.charAt (i) && str.charAt (i) <= 'Z') index = str.charAt (i) - 'A'; // If lowercase character, subtract 'a'to find index. else if ('a' <= str.charAt (i) && str.charAt (i) <= 'z') index = str.charAt (i) - 'a'; // Mark current character mark[index] = true; } // Return false if any character is unmarked for (int i = 0; i <= 25; i++) if (mark[i] == false) return (false); // If all characters were present return (true); } // Main Code public static void main (String[]args) { Scanner sc=new Scanner(System.in); System.out.println ("Enter a string"); String str=sc.next(); if (checkPangram (str) == true) System.out.print (str + " is a pangram."); else System.out.print (str + " is not a pangram."); } }
Output
Enter a string
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy dog is a pangram
#include
#include
int main()
{
char str[]=”Abcd efgh ijklmn opqrst uvwxyz”;
// gets(str);
int count=97, res=0,j; //ascii value of a is 97
strlwr(str); //lower case
puts(str);
int n=strlen(str);
while(count!=123) //ascii value z is 122
{
for(j=0;j<n;j++)
{
if(str[j]==count)
{
res++;
break;
}
}
count++;
}
if(res==26)
printf("Pangram");
else
printf("not pangram %d",res);
}
# Python One-Liner
“Not a pangram” if set(“abcdefghijklmnopqrstuvwxyz”) – set(input(“Please enter the string: “).lower()) else “Pangram”
static boolean pangram(String s){
s=s.replaceAll(” “,””);
s.toLowerCase();
Set rs=new HashSet();
for(int i=0;i<s.length();i++)
{
rs.add(s.charAt(i));
if(set.size()==26)
return true;
}
if(rs.size()==26)
return true;
return false;
}
a=str(input(“enter”))
b=a.lower()
c=list(b.replace(” “,””))
empty=[]
alpha=list(‘abcdefghijklmnopqrstuvwxyz’)
print(alpha)
for i in c:
if i in empty:
pass
elif (i.isalpha()!=True):
pass
else:
empty.append(i)
empty.sort()
if (empty==alpha):
print(“its a pangram”)
else:
print(“not a pangram”)
Java Solution for this problem
package test;
import java.util.Scanner;
public class panagram {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String s=sc.nextLine();
System.out.println(checkPanagram(s));
}
public static boolean checkPanagram(String s) {
String temp=s.toUpperCase().replace(” “,””);
//System.out.println(temp);
boolean a[]=new boolean[26];
for(int i=0;i<temp.length();i++) {
a[temp.charAt(i)-65]=true;
}
for(int i=0;i<26;i++) {
if(a[i]==false)
return false;
}
return true;
}
}
GOOD
My O(n) solution
import java.io.*;
import java.util.*;
class problem5{
public static void main(String[] args){
Scanner s=new Scanner(System.in);
String str=s.nextLine();
int[] a=new int[26];
for(int i=0;i<str.length();i++){
if(str.charAt(i)!=' ' && a[Character.toUpperCase(str.charAt(i))-'A']==0){
a[Character.toUpperCase(str.charAt(i))-'A']++;
}
}
for(int i=0;i<26;i++){
if(a[i]==0){
System.out.println(str+" is not a panagram");
return;
}
}
System.out.println(str+" is a panagram");
}
}
string=input(“”).lower()
str=[]
for i in string:
if(i not in str and i!=” “):
str.append(i)
str.sort()
if(str == [‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’, ‘g’, ‘h’, ‘i’, ‘j’, ‘k’, ‘l’, ‘m’, ‘n’, ‘o’, ‘p’, ‘q’, ‘r’, ‘s’, ‘t’, ‘u’, ‘v’, ‘w’, ‘x’, ‘y’, ‘z’]):
print(“PANGRAM”)
else:
print(“NOT A PANGRAM”)
#python
string=input()
string=string.lower()
string=set(string)
alpha=[ch for ch in string if ord(ch) in range(ord(‘a’),ord(‘z’)+1)]
if(len(alpha)==26):
print(“pangram”)
else:
print(“not pangram”)
public static void CheckPangram(String str) {
char[] letters=str.toCharArray();
HashMap hmap=new HashMap();
for(int i=0;i<letters.length;i++) {
if(letters[i]=’a’) {
if(hmap.containsKey(letters[i])) {
hmap.put(letters[i], hmap.get(letters[i])+1);
} else {
hmap.put(letters[i], 1);
}
}
}
if(hmap.size()==26) {
System.out.println(str+” is a pangram”);
} else {
System.out.println(str+” is not a pangram”);
}
}
import java.util.regex.Pattern;
import java.util.regex.Matcher;
import java.util.Scanner;
import java.util.HashSet;
public class tcsDigital {
String s;
tcsDigital()
{
Scanner scan=new Scanner(System.in);
s=scan.nextLine();
}
public boolean isAlpha(char temp)
{
Pattern p=Pattern.compile(“[a-z]”);
Matcher m=p.matcher(String.valueOf(temp));
return m.matches();
}
public boolean check()
{
HashSet h=new HashSet();
char []c=s.toCharArray();
for(int i=0;i<c.length;i++)
{
if(isAlpha(c[i]))
{
h.add(c[i]);
}
}
System.out.println(h.size());
System.out.println(h);
if(h.size()==26)
return true;
return false;
}
public static void main(String []args)
{
tcsDigital td=new tcsDigital();
if(td.check())
{
System.out.println("Is A Pangram!");
}
else
{
System.out.println("Not A Pangram!");
}
}
}
a=input()
count=0
b=’abcdefghijklmnopqrstuv’
l=len(b)
def pangram(ch):
global count
global a
charset=a
check=ch in charset
if check==True:
count+=1
else:
count=0
return count
for i in range(len(b)):
c=pangram(b[i])
if c==l:
print(‘yes’)
else:
print(‘no’)