# Count characters at same position as in English alphabets in C++

We are given a string of any length containing both uppercase and lowercase letters and the task is to compute the count of those characters that are at the same position as in english alphabets.

## For Example

Input − String str = eBGD
Output − Count is: 2

Explanation − B and D are the characters that lie in the same order in english alphabets as B comes at second position and D comes at fourth position.

Input − String str = Abcdeizxy
Output − Count is: 5

Explanation − A, B, C, D and E are the characters that lie in the same order in english alphabets as A comes at first position then B, C, D and E.

## Approach used in the below program is as follows

• Input the string consisting of uppercase and lowercase letters.

• Start the loop from 0 to size of a string which can be calculated using the size() function.

• Now check if ‘i = str[i] - ‘a’ OR i = str[i] - ‘a’ ’ as our string contains both uppercase and lowercase letters.

• Now, take a temporary variable let’s say temp, initialise it with 0, outside the loop and start incrementing it with 1 inside the loop

• Return the value in temp

• Print the result.

## Example

Live Demo

#include<iostream>
using namespace std;
int countalphabet(string str){
int res= 0;
// Traverse the string
for (int i = 0 ; i < str.size(); i++){
// As all uppercase letters are grouped together
// and lowercase are grouped together so
// if the difference is equal then they are same
if (i == (str[i] - 'a') || i == (str[i] - 'A')){
res++;
}
}
return res;
}
// main function
int main(){
string str = "aBTutorIalspOiNT";
cout <<”Count is:”<< countalphabet(str);
return 0;
}

## Output

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

Count is: 2