Count and Print the alphabets having ASCII value not in the range [ l , r ] | C ++ implementation of the above approach ; Function to count the number of characters whose ascii value not in range [ l , r ] ; Initializing the count to 0 ; using map to print a character only once ; Increment the count if the value is less ; return the count ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int CountCharacters ( string str , int l , int r ) { int cnt = 0 ; unordered_map < char , int > m ; int len = str . length ( ) ; for ( int i = 0 ; i < len ; i ++ ) { if ( ! ( l <= str [ i ] and str [ i ] <= r ) ) { cnt ++ ; if ( m [ str [ i ] ] != 1 ) { cout << str [ i ] << " ▁ " ; m [ str [ i ] ] ++ ; } } } return cnt ; } int main ( ) { string str = " geeksforgeeks " ; int l = 102 , r = 111 ; cout << " Characters ▁ with ▁ ASCII ▁ values " " ▁ not ▁ in ▁ the ▁ range ▁ [ l , ▁ r ] ▁ STRNEWLINE in ▁ the ▁ given ▁ string ▁ are : ▁ " ; cout << " and their count is " << CountCharacters ( str , l , r ) ; return 0 ; }