Skip to content
Related Articles

Related Articles

Improve Article

Count and Print the alphabets having ASCII value not in the range [l, r]

  • Last Updated : 26 May, 2021

Given a string str, the task is to count the number of alphabets having ASCII values, not in the range [l, r].
Examples: 
 

Input: str = "geeksforgeeks", l = 102, r = 111
Output: Count = 7
Characters - e, s, r have ascii values not in the range [102, 111].

Input: str = "GeEkS", l = 80, r = 111
Output: Count = 2

 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

Approach: Start traversing the string and check if the current character has ASCII value less than equal to r and greater than equal to l. If no then increment the count and print that element.
Below is the implementation of above approach:
 

C++




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

Java




// Java implementation of the above approach
import java.util.*;
class Solution
{
   
// Function to count the number of characters
// whose ascii value not in range [l, r]
static int CountCharacters(String str, int l, int r)
{
    // Initializing the count to 0
    int cnt = 0;
   
    // using map to print a character only once
    Map<Character, Integer> m= new HashMap<Character, Integer>();
    int len = str.length();
    for (int i = 0; i < len; i++) {
   
        // Increment the count
        // if the value is less
        if (!(l <= str.charAt(i) && str.charAt(i) <= r)) {
            cnt++;
            if(m.get(str.charAt(i))!=null)
            if (m.get(str.charAt(i)) != 1) {
                System.out.print(str.charAt(i) + " ");
            m.put(str.charAt(i),m.get(str.charAt(i))==null?0:m.get(str.charAt(i))+1);
            }
        }
    }
    // return the count
    return cnt;
}
   
// Driver code
public static void main(String args[])
{
    String str = "geeksforgeeks";
    int l = 102, r = 111;
   
    System.out.println( "Characters with ASCII values not in the range [l, r] \nin the given string are: ");
     System.out.println(  "\nand their count is " + CountCharacters(str, l, r));
   
}
}
//contributed by Arnab Kundu

Python3




# Python3 implementation of the
# above approach
 
# Function to count the number of
# characters whose ascii value not
# in range [l, r]
def CountCharacters(str, l, r):
 
    # Initializing the count to 0
    cnt = 0
 
    # using map to pra character
    # only once
    m = {}
    length = len(str)
    for i in range(0, length):
         
        # Increment the count if the
        # value is less
        if (not(l <= ord(str[i]) and
                     ord(str[i]) <= r)):
            cnt += 1
            if ord(str[i]) not in m:
                m[ord(str[i])] = 0
                print(str[i], end = " ")
            m[ord(str[i])] += 1
             
    # return the count
    return cnt
 
# Driver Code
if __name__ == '__main__':
 
    str = "geeksforgeeks"
    str = str.strip()
    l = 102
    r = 111
    print("Characters with ASCII values", end = "")
    print(" not in the range [l, r]\n",
          "in the given string are: ", end = "")
    print("\nand their count is ",
            CountCharacters(str, l, r))
 
# This code is contributed by
# Shubham Singh(SHUBHAMSINGH10)

C#




// C# implementation of the above approach
using System;
using System.Collections;
using System.Collections.Generic;
 
class GFG
{
    
// Function to count the number of characters
// whose ascii value not in range [l, r]
static int CountCharacters(string str, int l, int r)
{
   
    // Initializing the count to 0
    int cnt = 0;
    
    // using map to print a character only once
    Dictionary<char, int> m = new Dictionary<char, int>();
    int len = str.Length;
    for (int i = 0; i < len; i++)
    {
    
        // Increment the count
        // if the value is less
        if (!(l <= str[i] && str[i] <= r))
        {
            cnt++;
            if(!m.ContainsKey(str[i]))
            {
                m[str[i]] = 0;
                Console.Write(str[i] + " ");
            }
            m[str[i]]++;
        }
    }
   
    // return the count
    return cnt;
}
    
// Driver code
public static void Main(string []args)
{
    string str = "geeksforgeeks";
    int l = 102, r = 111;   
    Console.Write( "Characters with ASCII values" +
                  "not in the range [l, r] \nin" +
                  "the given string are: ");
     Console.WriteLine( "\nand their count is " +
                       CountCharacters(str, l, r));
}
}
 
// This code is contributed by rutvik_56

PHP




<?php
// PHP implementation of the above approach
 
// Function to count the number of characters
// whose ascii value not in range [l, r]
function CountCharacters($str, $l, $r)
{
    // Initializing the count to 0
    $cnt = 0;
 
    // using map to print a character
    // only once
    $m = array_fill(0, 256, NULL);
    $len = strlen($str);
    for ($i = 0; $i < $len; $i++)
    {
 
        // Increment the count
        // if the value is less
        if (!($l <= ord($str[$i]) and
                    ord($str[$i]) <= $r))
        {
            $cnt++;
            if (isset($m[ord($str[$i])]) != 1)
            {
                echo $str[$i] . " ";
                $m[ord($str[$i])]++;
            }
        }
    }
    // return the count
    return $cnt;
}
 
// Driver code
$str = "geeksforgeeks";
$l = 102;
$r = 111;
echo "Characters with ASCII values not in the " .
      "range [l, r] \nin the given string are: ";
echo "\nand their count is " .
       CountCharacters($str, $l, $r);
 
// This code is contributed
// by ChitraNayal
?>

Javascript




<script>
 
// JavaScript implementation of the above approach
 
// Function to count the number of characters
// whose ascii value not in range [l, r]
function CountCharacters(str,l,r)
{
    // Initializing the count to 0
    let cnt = 0;
    
    // using map to print a character only once
    let m= new Map();
    let len = str.length;
    for (let i = 0; i < len; i++) {
    
        // Increment the count
        // if the value is less
        if (!(l <= str[i].charCodeAt(0) &&
        str[i].charCodeAt(0) <= r)) {
            cnt++;
            if(!m.has(str[i]))
            {
                m.set(str[i],0);
                document.write(str[i] + " ");
            }   
            m.set(str[i],m.get(str[i]+1));
        }
    }
    // return the count
    return cnt;
}
 
// Driver code
 
let str = "geeksforgeeks";
let l = 102, r = 111;
 
document.write( "Characters with ASCII values " +
"not in the range [l, r] <br>in the given string are: ");
 
document.write(  "<br>and their count is " +
CountCharacters(str, l, r));
 
// This code is contributed by avanitrachhadiya2155
 
</script>
Output:
Characters with ASCII values not in the range [l, r] 
in the given string are: e s r 
and their count is 7

 

Time Complexity: O(n) where n is the length of string.
 




My Personal Notes arrow_drop_up
Recommended Articles
Page :