In: Computer Science
Write a C++ function to print out all unique letters of a given string. You are free to use any C++ standard library functions and STL data structures and algorithms and your math knowledge here. Extend your function to check whether a given word is an English pangram (Links to an external site.). You may consider your test cases only consist with English alphabetical characters and the character.
#include <iostream>
using namespace std;
void function(string str){
// Array to store 0 and 1 if a is present in string a[0]=1,if b is
present a[1] = 1,
// if b is absent a[1] = 0 and so on.
int a[26] = {0};
cout<<"Unique letters : ";
//Iterating all the letters of string
for(int i=0;i<str.length();i++){
//Converting each letter to lower case if it is in upper
case.
if(isupper(str[i]))
str[i] = tolower(str[i]);
// 'a'-'a'=0, 'b'-'a'=1 this way, w'll get index of letter in array
a by str[i]-'a'
//checking if letter was already present in the string
if(a[str[i]-'a'] == 0){
//updating array a
a[str[i]-'a'] = 1;
//printing letter
cout<<str[i];
}
}
// To check if string is pangram
// Assuming that string is pangram
bool pangram = true;
//iterating array a
for(int i=0;i<26;i++){
//if any of the letter is absent, then string is not pangram
if(a[i] == 0){
pangram = false;
break;
}
}
// if pangram is true, it means string is pangram
if(pangram){
cout<<"\n String is pangram";
}
// if pangram is false, it means string is not pangram
else{
cout<<"\n String is not pangram";
}
}
int main()
{
cout<<"Enter String : ";
string str;
//Input of String
cin>>str;
//Calling method to print unique letters and checking for
pangram
function(str);
return 0;
}
OUTPUT: