user14688876
user14688876

Reputation:

How could I get the letter position into alphabet?

I need to know the position of the letter within the alphabet.

Is there a simple way to do this without using FOR loop?

word[0] = hello; //H

H = position 8 into alphabet
E = position 5 into alphabet

Thank you

Upvotes: 0

Views: 291

Answers (1)

user9706
user9706

Reputation:

If the letter is upper case subtract the value of 'A' and if it's lower case 'a'. Alternatively, use toupper or tolower on the letter and use either:

uint8_t position(char c) {
   c = tolower(c);
   assert(c >= 'a' && c <= 'z');
   return c - 'a';
}

As @WhozCraig points this only works in a special case, and when you start dealing with UTF-8, a letter may not even be a single octet.

Upvotes: 2

Related Questions