Is there a quick way to retrieve given character\'s position in the english alphabet in C?
Something like:
int position = get_position(\'g\');
You should also probably take into account upper/lower case. In my expereince, counting from 1, is often dangerous because it can lead to off-by-one bugs. As a rule of thumb I always convert to a 1-based index only when interacting with the user, and use 0-based counting internally, to avoid confusion.
int GetPosition(char c)
{
if (c >= 'a' && c <= 'z') {
return c - 'a';
}
else if (c >= 'A' && c <= 'Z') {
return c - 'A';
}
else {
// Indicate that it isn't a letter.
return -1;
}
}