codeforces Pangram in c++
Here is an example of a C++ program to check if a given string is a pangram:
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
bool isPangram(string str) {
transform(str.begin(), str.end(), str.begin(), ::tolower);
bool alphabets[26] = {false};
for (char c : str) {
if (c >= 'a' && c <= 'z') {
alphabets[c - 'a'] = true;
}
}
for (bool b : alphabets) {
if (!b) {
return false;
}
}
return true;
}
int main() {
string str;
cout << "Enter a string: ";
getline(cin, str);
if (isPangram(str)) {
cout << "The string is a pangram." << endl;
} else {
cout << "The string is not a pangram." << endl;
}
return 0;
}
Explanation:
The
isPangram
function takes a string as input and returns a boolean value indicating whether the string is a pangram or not. A pangram is a sentence that contains every letter of the alphabet at least once.The
transform
function is used to convert all characters in the string to lowercase. This is done to handle both uppercase and lowercase letters in the input string.The
alphabets
array is a boolean array of size 26, initialized withfalse
values. Each index in the array represents a letter of the alphabet, starting from 'a' at index 0 to 'z' at index 25.The loop iterates over each character in the input string. If the character is a lowercase letter, its corresponding index in the
alphabets
array is set totrue
.After iterating through all the characters, another loop checks if any of the elements in the
alphabets
array is stillfalse
. If any element isfalse
, it means that the corresponding letter is missing in the input string, and the function returnsfalse
.If all elements in the
alphabets
array aretrue
, it means that all letters of the alphabet are present in the input string, and the function returnstrue
.In the
main
function, the user is prompted to enter a string. Thegetline
function is used to read the entire line, including spaces.The
isPangram
function is called with the user's input string as an argument. If it returnstrue
, the program displays the message "The string is a pangram." If it returnsfalse
, the program displays the message "The string is not a pangram."Finally, the program returns 0 to indicate successful execution.