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

C++Server Side ProgrammingProgramming

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

 A B C D E F G H I J K L M N O P Q R S 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83

 T U V W X Y Z 84 85 86 87 88 89 90

## ASCII value for characters a-z are given below

 a b c d e f g h i j k l m n o p q r s 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 114

 t u v w x y z 116 117 118 119 120 121 122

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
Published on 31-Aug-2020 11:28:00