C++ STL for finding Alphabet Position

How to get the character's position of the alphabet in C++ language?

How do I find the position of the alphabet?

Like the position of A is 1, B is 2, C is 3, D is 4, and so on. Sometimes in the code, we need this value.

So to find this there is a very easy way. Let us look at it.

To get it we can find the ASCII value of the alphabet and then we can subtract 64 from it for the uppercase Alphabet and for lowercase we need to subtract 96 from it.

Or First, you can convert the string to upper or lower case and then can find it.

Let there be a string s.


and we need the sum of every alphabet position so to find it we can traverse through the string s and add it.

#include <bits/stdc++.h>
using namespace std;
int main()
string s = "abcdef";
int ans = 0;
for (int i = 0; i < s.length(); i++)
//Finding the position of the alphabet and adding to the ans.
ans = ans + (s[i] - 96);
cout << ans;
return 0;

Leet Code Question Where this can be Implemented is-  Excel Sheet Column Number

Solution to the Above question is given below

class Solution {
    int titleToNumber(string s) {
    long long ans=0;
        long long pow=1;
        int n=s.length();
        for(int i=n-1; i>=0; i--){
        return ans;

Thank You for Visiting This Page
Comment Down Below for Suggestions

Post a Comment