Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert character to its alphabet integer position?

I'm trying to find if there is a quick way to get the integer position of a character in the alphabet (C#).

I can simply create an array and get the position, but seems there must be a "nice and funky" way of acheiving this?

I've also looked at taking the ASCII position of the (uppercase) character in relation to "65"...but again, seems more work than it should be!

[English 26 letter alphabet only, no internationalisation required - and no, this is not homework!]

like image 590
BlueChippy Avatar asked Nov 18 '13 09:11

BlueChippy


People also ask

How do you convert letters to integers in Python?

To convert Python char to int, use the ord() method. The ord() is a built-in Python function that accepts a character and returns the ASCII value of that character. The ord() method returns the number representing the Unicode code of a specified character.

How do you convert characters to numbers?

Way 2: Using valueOf() method of String class The method valueOf() of class String can convert various types of values to a String value. It can convert int, char, long, boolean, float, double, object, and char array to String, which can be converted to an int value by using the Integer. parseInt() method.

How do you get the numeric position of the alphabet in C++?

int position = 'g' - 'a' + 1; In C, char values are convertible to int values and take on their ASCII values. In this case, 'a' is the same as 97 and 'g' is 103. Since the alphabet is contiguous within the ASCII character set, subtracting 'a' from your value gives its relative position.


1 Answers

Programming 101:

char c = 'A'; //char c = 'b'; you may use lower case character. int index = char.ToUpper(c) - 64;//index == 1 
like image 90
Ahmed KRAIEM Avatar answered Oct 16 '22 06:10

Ahmed KRAIEM