Code:
int to_array_index(int c) {
if (c >= 97 && c <= 122) /* ascii a - b */
{
return c - 97;
}
else if (c >= 65 && c <= 90) /* ascii A - B */
{
return c - 65
}
else {
return -1; /* error, not letter */
}
}
Yeuch...
Code:
int to_array_index(int c) {
if (c >= 'a' && c <= 'z') /* ascii a - b */
{
return c - 'a';
}
else if (c >= 'A' && c <= 'Z') /* ascii A - B */
{
return c - 'A'
}
else {
return -1; /* error, not letter */
}
}
is much easier to read, don't you think?
Yes, I know that 97 is 'a', but why not let the compiler work it out for you. The one extra character you have to type in 5 out of the 6 places isn't really going to matter in the whole scheme of things, is it?
--
Mats