Converting an alphabet to its position number in the alphabet

Help please I need a simple c++ code that outputs the position of an alphabet e.g that E is position 5 in the alphabet.

Here is what i did:

#include<iostream>
#include<string>
using namespace std;

int main()
{
char x;
int result;
cout<<"Enter any character to get its position in the alphabet: "<<endl;
cin>>x;
result=x;
cout<<x<<" is in position "<<result<<" in the alphabet"<<endl;
system("pause");
}

but the problem with this output is that it gives me the ASCII value. can someone help!!
Just subtract the difference between the number you want and the ASCII value.

If you're working with upper case characters, it'll be 64.

The ASCII code for 'A' is 65. So, subtracting 64 gives 1.
The ASCII code for 'T' is 84. So, subtracting 64 gives 20.

Just make sure you put a toupper or tolower function in there to make sure that you're going to be working from the right ASCII value (the upper or lower case - your choice).

EDIT: You'll find toupper/tolower in the ctype header.
Last edited on
@iHutch thanks now its working.
Topic archived. No new replies allowed.