Skip to main content

A C++ program to check if a string is a pangram

I'm new to C++ and decided to implement a simple program that checks if a string is a pangram (i.e. the string contains all the English alphabet letters). It's my first program in C++ and I have some background with interpreted programming languages like Python and R.

I want to know what are the common beginners mistakes I'm making in this implementation, whether I'm following good practices, what I should avoid etc.

The Code

#include<iostream>
#include<string>
#include<vector>
#include <algorithm>


bool isPangram(std::string str){
    


    std::transform(str.begin(), str.end(), str.begin(), ::toupper); // Transforming a string to upper case
    std::vector<bool> hash_table(26, false); // a hash_table that maps the  index of a letter from the alphabet
                                             // and a boolean saying if the string contains this element                                             
    int index;

    for(auto c: str){
        
        if(isalpha(c))
            // Checks if the element in the string is in the alphabet
            index = c - 'A';
            hash_table[index] = true; 
            std::cout << index << std::endl;

    };

    if(std::any_of(hash_table.begin(), hash_table.end(), [](bool v) { return !v; })) {
        // if any of the items in the array evaluates to false
        // means that the string don't contain all the elements 
        // in the alphabet,hence, it's not a pangram

        return false;
    }

    return true;    
};


int main() {

    std::string str = "We promptly judged antique ivory buckles for the next prize"; 

     if (isPangram(str)){ 
         std::cout << "Yes" << std::endl; 
     }
     else {
         std::cout << "No" << std::endl; 
           
     };
     return 0;
    

}




```
Occhima
  • 273
  • 2
  • 8