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
PYTHON CODE
k=list(input().split())
r=(“”.join(k)).lower()
s=set(r)
print(len(s)==26)
li = “abcdefghijklmnopqrs”
string = input()
x=0
for i in li:
if not i in string.lower():
x +=1
if x==0:
print(“String is Pangram”)
else:
print(“String is not Pangram”)
## Python code
sent = “qqwertyuioplkjhgfdsazxcvbn”
sent = sent.lower()
sent = sent.replace(” “,””)
sent = set(sent)
if(len(sent) == 26):
print(True)
else:
print(False)
JAVA AND PYTHON
import java.util.*;
public class Main
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int count1=0;
/*String 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”};*/
String str = “ABCDEFGHIJKLMNOPQRSTUVWXYZ”;
String str1 = sc.nextLine().toUpperCase();
for (int i=0;i<str.length();i++){
int count=0;
for (int j=0;j=1){
count1 = count1+1;
}
}
System.out.println(count1);
if(count1==26){
System.out.println(“pangram”);
}
else{
System.out.println(“Not a pangram”);
}
}
}
/*************************************PYTHON**************************************/
i_str = “ABCDEFGHIJKLMNOPQRSTUVWXYZ”
i_str1 = input().upper()
count1 = 0
for i in range (len(i_str)):
count = 0;
for j in range (len(i_str1)):
if(i_str[i] == i_str1[j]):
count = count+1
if(count>=1):
count1 = count1+1
print(count1)
if(count1 == 26):
print(“pangram”)
else:
print(“Not a pangram”)
PYTHON:
inp = input(“enter the input string:”)
inp.lower()
l = len(inp)
print(inp,l)
count = 0
alphabet =[‘aA’,’bB’,’cC’,’dD’,’eE’,’fF’,’gG’,’hH’,’iI’,’jJ’,’kK’,’lL’,’mM’,’nN’,’oO’,’pP’,’qQ’,’rR’,’sS’,’tT’,’uU’,’vV’,’wW’,’xX’,’yY’,’zZ’]
le=len(alphabet)
#print(len(alphabet[0]))
for i in range(le):
for j in range(len(alphabet[i])):
if alphabet[i][j] in inp:
count+=1
print(count)
if count==26:
print(“The String is a Pangram”)
else:
print(“The String is not a Pangram”)
Different python code:
inp = input(“enter the input string:”)
l = len(inp)
print(inp,l)
count = 0
alphabet =[‘aA’,’bB’,’cC’,’dD’,’eE’,’fF’,’gG’,’hH’,’iI’,’jJ’,’kK’,’lL’,’mM’,’nN’,’oO’,’pP’,’qQ’,’rR’,’sS’,’tT’,’uU’,’vV’,’wW’,’xX’,’yY’,’zZ’]
le=len(alphabet)
#print(len(alphabet[0]))
for i in range(le):
for j in range(len(alphabet[i])):
if alphabet[i][j] in inp:
count+=1
print(count)
if count==26:
print(“The String is a Pangram”)
else:
print(“The String is not a Pangram”)
#include
#include
using namespace std;
int main()
{
char str[200];
int i=0, chk=0, val= 0, a[26] = {0}, sum = 0;
gets(str);
while(str[i]!=’\0′)
{
val = 0;
if(str[i]!=’ ‘)
{
val = (int)tolower(str[i]) – 97;
a[val]++;
}
i++;
}
for(int i=0; i < 26; i++)
{
cout << a[i] <= 1)
sum++;
}
if ( sum == 26)
cout << "Pangrams";
else
cout << "Not Pangrams";
cout<<endl;
return 0;
}
str = input().lower()
l = list(‘abcdefghijklmnopqrstuvwxyz”) or
import string
l = list(string.ascii_lowercase)
c=0
for each in l:
if each in str:
c+=1
if c==26:
print(‘Pangram’)
else:
print(‘Not Pangram’)
PYTHON code
from string import ascii_lowercase
def pangram(s):
if (set(ascii_lowercase).issubset(s)):
return print(‘This is a pangram’)
else:
return print(‘This is not a pangram’)
s = input()
s = s.lower()
pangram(s)
str = “The quick brown fox jumps over the lazy dog”
l=[‘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’]
dict1={}
dict2={}
for ele in l:
dict1[ele]=1
for ele in str.lower():
if(ele!=’ ‘):
dict2[ele]=1
if(dict1==dict2):
print(‘The String is a Pangram’)
else:
print(‘The String is Not a Pangram’)
all_alphabets = [‘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’]
string = input(‘Enter String: ‘)
string_in_lower_case = string.lower().replace(” “, “”)
def check_pangram(entered_string):
all_alphabets_in_String = []
for i in entered_string:
if (i not in all_alphabets_in_String):
all_alphabets_in_String.append(i)
if (len(all_alphabets_in_String) == len(all_alphabets)):
print(‘Pangram’)
else:
print(‘Not pangram’)
check_pangram(entered_string=string_in_lower_case)
python