Count of alphabets having ASCII value less than and greater than k in C++


We are given a string of any length and the task is to calculate the count of alphabets having ASCII values less than or greater than or equals to the given integer value k.

ASCII value for character A-Z are given below

ABCDEFGHIJKLMNOPQRS
65666768697071727374757677787980818283


TUVWXYZ
84858687888990

ASCII value for characters a-z are given below

abcdefghijklmnopqrs
979899100101102103104105106107108109110111112113114114


tuvwxyz
116117118119120121122

Input − str = “TuTorials PoinT”, int k = 100

Output

Count of alphabets having ASCII value less than k are − 6

Count of alphabets having ASCII value equals or greater than k are − 9

Explanation

We are given with k as 100 so we will check ASCII values of the characters in the string. So, ASCII value of T is 84 < 100, u is 117 > 100, o is 111 > 100, r is 114 > 100, i is 105 > 100, a is 97 < 100, l is 108 > 100, s is 115 > 100, P is 80 < 100, n is 110 > 100. Therefore, total count of alphabets having ASCII value less than k are 6 and total count of alphabets having ASCII value equals or greater than k are 9

Input − str = “HellO All”, int k = 90

Output

Count of alphabets having ASCII value less than k are − 3

Count of alphabets having ASCII value equals or greater than k are − 5

Explanation

We are given with k as 100 so we will check ASCII values of the characters in the string. So, ASCII value of H is 72 < 90, e is 101 > 90, l is 108 > 100, l is 108 > 100, O is 79 < 90, A is 65 < 90, l is 108 > 100, l is 108 > 100. Therefore, total count of alphabets having ASCII value less than k are 3 and total count of alphabets having ASCII value equals or greater than k are 5

Approach used in the below program is as follows

  • Input the string of uppercase and lowercase letters and an integer values of k

  • Calculate the length of a string using str.length() function and pass it to the function for further processing.

  • Create a temporary variable count to store the value less than k and set it to 0

  • Start loop FOR from i to 0 till the length of a string

  • Inside the loop, check IF str[i] < k then increments the count by 1.

  • Return the count for values less than k

  • To calculate the count for values greater than k set int greater = len - Less_than(str,k, len);

  • Print the result.

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
//Count of alphabets having ASCII value less than and greater than k
int Less_than(string str, int k, int length){
   int count = 0;
   for (int i = 0; i < length; i++){
      if (str[i] < k){
         count++;
      }
   }
   return count;
}
int main(){
   string str = "TuTorials PoinT";
   int k = 100;
   int len = str.length();
   cout<<"Count of alphabets having ASCII value less than k are: "<<Less_than(str,k, len);
   int greater = len - Less_than(str,k, len);
   cout<<"\nCount of alphabets having ASCII value equals or greater than k are: "<<greater;
   return 0;
}

Output

If we run the above code it will generate the following output −

Count of alphabets having ASCII value less than k are: 6
Count of alphabets having ASCII value equals or greater than k are: 9

Updated on: 31-Aug-2020

208 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements