Pangram string program in C++ | How to check a string is pangram or not?

Pangram string program in C++

        English has a total of 26 alphabets. If a string comprises all the English alphabets in it then it is called a pangram. A pangram is a popular program in interviews. This program can be executed in several ways but I will use a simple and easy-to-understand approach here. 


Code:

#include <bits/stdc++.h>
using namespace std;

int main()
{
    string s="abcdefghijklmnopqrstuvwxyz";      //create a string with all alphabets
    string p;                                                           //input string
    getline(cin,p);
    
    transform(p.begin(), p.end(), p.begin(), ::tolower);        //to convert all string alphabets to lower
    
    int flag;                                 //to check conditions
    
    for(int i=0;i<s.length();i++){              //first loop to traverse s string
        flag=0;
        for(int j=0;j<p.length();j++){          //second loop to traverse p string
            if(s[i]==p[j]){                     //if match found break the loop
                flag=0;
                break;
            }
            else{                               //otherwise continue
                flag=1;
            }
        }
//if no match found for a particular alphabet in p string, break the outer loop
        if(flag==1){                        
            break;
        }
    }
    if(flag==1){                                    //flag 1 all alphabets are not present
        cout<<"Given String is not a Pangram";
    }
    else{                                         //flag 0 all alphabets are present
        cout<<"Given string is Pangram";
    }

    return 0;
}



Output

coding demons
Given String is not a Pangram



Post a Comment

0 Comments