Skip to content
Related Articles

Related Articles

Improve Article

Count of alphabets having ASCII value less than and greater than k

  • Last Updated : 03 May, 2021
Geek Week

Given a string, the task is to count the number of alphabets having ASCII values less than and greater than or equal to a given integer k.
 

Examples: 

Input: str = "GeeksForGeeks", k = 90
Output: 3, 10
G, F, G have ascii values less than 90.
e, e, k, s, o, r, e, e, k, s have ascii values 
greater than or equal to 90

Input: str = "geeksforgeeks", k = 90
Output: 0, 13 

Approach: Start traversing the string and check if the current character has an ASCII value less than k. If yes then increment the count. 
So, the Remaining characters will have ASCII values greater than or equal to k. So, print len_of_String – count for characters with ASCII values greater than or equal to k.
Below is the implementation of the 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 is less than k
int CountCharacters(string str, int k)
{
    // Initialising the count to 0
    int cnt = 0;
 
    int len = str.length();
    for (int i = 0; i < len; i++) {
        // Incrementing the count
        // if the value is less
        if (str[i] < k)
            cnt++;
    }
 
    // return the count
    return cnt;
}
 
// Driver code
int main()
{
    string str = "GeeksForGeeks";
    int k = 90;
 
    int count = CountCharacters(str, k);
    cout << "Characters with ASCII values"
            " less than K are "
         << count;
 
    cout << "\nCharacters with ASCII values"
            " greater than or equal to K are "
         << str.length() - count;
 
    return 0;
}

Java




// Java implementation of the above approach
import java.util.*;
 
class GFG {
     
// Function to count the number of
// characters whose ascii value is less than k
static int CountCharacters(String str, int k)
{
    // Initialising the count to 0
    int cnt = 0;
 
    int len = str.length();
    for (int i = 0; i < len; i++) {
        // Incrementing the count
        // if the value is less
        if (((int)str.charAt(i)) < k)
            cnt++;
    }
 
    // return the count
    return cnt;
}
 
// Driver code
public static void main(String args[])
{
    String str = "GeeksForGeeks";
    int k = 90;
 
    int count = CountCharacters(str, k);
    System.out.println("Characters with ASCII values less than K are "+count);
 
    System.out.println("Characters with ASCII values greater than or equal to K are "+(str.length() - count));
 
}
}

Python3




# Python3 implementation of the
# above approach
 
# Function to count the number of
# characters whose ascii value is
# less than k
def CountCharacters(str, k):
 
    # Initialising the count to 0
    cnt = 0
 
    l = len(str)
    for i in range(l):
         
        # Incrementing the count
        # if the value is less
        if (ord(str[i]) < k):
            cnt += 1
 
    # return the count
    return cnt
 
# Driver code
if __name__ == "__main__":
 
    str = "GeeksForGeeks"
    k = 90
 
    count = CountCharacters(str, k)
    print ("Characters with ASCII values",
                "less than K are", count)
 
    print ("Characters with ASCII values",
           "greater than or equal to K are",
                           len(str) - count)
 
# This code is contributed by ita_c

C#




// C# implementation of the above approach
using System;
class GFG {
     
// Function to count the number of
// characters whose ascii value is less than k
static int CountCharacters(String str, int k)
{
    // Initialising the count to 0
    int cnt = 0;
 
    int len = str.Length;
    for (int i = 0; i < len; i++)
    {
        // Incrementing the count
        // if the value is less
        if (((int)str[i]) < k)
            cnt++;
    }
 
    // return the count
    return cnt;
}
 
// Driver code
public static void Main()
{
    String str = "GeeksForGeeks";
    int k = 90;
    int count = CountCharacters(str, k);
    Console.WriteLine("Characters with ASCII values" +
                        "less than K are " + count);
 
    Console.WriteLine("Characters with ASCII values greater" +
                    "than or equal to K are "+(str.Length - count));
}
}
 
// This code is contributed by princiraj1992

PHP




<?php
// PHP implementation of the above approach
 
// Function to count the number of
// characters whose ascii value is less than k
function CountCharacters($str, $k)
{
    // Initialising the count to 0
    $cnt = 0;
 
    $len = strlen($str);
    for ($i = 0; $i < $len; $i++)
    {
        // Incrementing the count
        // if the value is less
        if ($str[$i] < chr($k))
            $cnt += 1;
    }
 
    // return the count
    return $cnt;
}
 
// Driver code
$str = "GeeksForGeeks";
$k = 90;
 
$count = CountCharacters($str, $k);
echo("Characters with ASCII values" .
       " less than K are " . $count);
 
echo("\nCharacters with ASCII values" .
     " greater than or equal to K are " .
                (strlen($str) - $count));
 
// This code contributed by Rajput-Ji
?>

Javascript




<script>
// Javascript implementation of the above approach
     
    // Function to count the number of
    // characters whose ascii value is less than k
    function CountCharacters(str,k)
    {
        // Initialising the count to 0
    let cnt = 0;
   
    let len = str.length;
    for (let i = 0; i < len; i++) {
        // Incrementing the count
        // if the value is less
        if (str[i].charCodeAt(0) < k)
            cnt++;
    }
   
    // return the count
    return cnt;
    }
     
    // Driver code
    let str = "GeeksForGeeks";
    let k = 90;
    let count = CountCharacters(str, k);
    document.write("Characters with ASCII values less than K are "+count+"<br>");
     
    document.write("Characters with ASCII values greater than or equal to K are "+(str.length - count));
     
    // This code is contributed by avanitrachhadiya2155
</script>
Output: 
Characters with ASCII values less than K are 3
Characters with ASCII values greater than or equal to K are 10

 

Time Complexity: O(N)
 

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.




My Personal Notes arrow_drop_up
Recommended Articles
Page :