S
Stefan Krah
Hello,
I am currently writing code where it is convenient to convert
char [a-zA-Z] to int [0-25]. The conversion function relies on
a character set with contiguous alphabets.
int set_mesg(Key *key, char *s)
{
char *x;
if (strlen(s) != 3)
return 0;
x = s;
while (*x != '\0') {
if (!isalpha(*x))
return 0;
*x = tolower(*x);
x++;
}
x = s;
/* l_mesg, m_mesg, r_mesg are int */
key->l_mesg = *x++ - 'a';
key->m_mesg = *x++ - 'a';
key->r_mesg = *x - 'a';
return 1;
}
According to K&R2 (p43) contiguous alphabets cannot be safely assumed.
This function would test the lowercase alphabet:
int cont_lower_alpha(void)
{
char a[26] = "abcdefghijklmnopqrstuvwxyz";
int i;
for (i = 0; i < 26; i++)
if (a - 'a' != i)
return 0;
return 1;
}
Is there an easier way of doing this?
Stefan Krah
I am currently writing code where it is convenient to convert
char [a-zA-Z] to int [0-25]. The conversion function relies on
a character set with contiguous alphabets.
int set_mesg(Key *key, char *s)
{
char *x;
if (strlen(s) != 3)
return 0;
x = s;
while (*x != '\0') {
if (!isalpha(*x))
return 0;
*x = tolower(*x);
x++;
}
x = s;
/* l_mesg, m_mesg, r_mesg are int */
key->l_mesg = *x++ - 'a';
key->m_mesg = *x++ - 'a';
key->r_mesg = *x - 'a';
return 1;
}
According to K&R2 (p43) contiguous alphabets cannot be safely assumed.
This function would test the lowercase alphabet:
int cont_lower_alpha(void)
{
char a[26] = "abcdefghijklmnopqrstuvwxyz";
int i;
for (i = 0; i < 26; i++)
if (a - 'a' != i)
return 0;
return 1;
}
Is there an easier way of doing this?
Stefan Krah