How to decide whether it is an English letter or a number?

Q

QQ

for instance, I read a char from the input
and I need to decide whether it is a letter or a number
What I am doing is
char a;
...... // read a
int true = false;
if(( (a >='0') && (a <='9')) | | ((a >='a') && (a <= 'z')) ||((a >='A')
&& (a <= 'Z')))
true = 1;

Is there any easier way for it?
Thanks a lot!
 
D

Dale

QQ said:
for instance, I read a char from the input
and I need to decide whether it is a letter or a number
What I am doing is
char a;
..... // read a
int true = false;
if(( (a >='0') && (a <='9')) | | ((a >='a') && (a <= 'z')) ||((a >='A')
&& (a <= 'Z')))
true = 1;

Is there any easier way for it?
Thanks a lot!

<hint>
Try looking in ctypes.h.
</hint>
 
F

Fred Kleinschmidt

QQ said:
for instance, I read a char from the input
and I need to decide whether it is a letter or a number
What I am doing is
char a;
..... // read a
int true = false;
if(( (a >='0') && (a <='9')) | | ((a >='a') && (a <= 'z')) ||((a >='A')
&& (a <= 'Z')))
true = 1;

Is there any easier way for it?
Thanks a lot!
isdigit(), isalpha(), etc.
 
O

Old Wolf

Dale said:
<hint>
Try looking in ctypes.h.
</hint>

If that doesn't work, try <ctype.h> .

Also, read the documentation carefully: those functions accept
a value in the range [0, UCHAR_MAX]; if you have some plain
or signed chars then you have to cast them to unsigned char
before passing them.
 
D

Dale

Old said:
Dale said:
<hint>
Try looking in ctypes.h. ^^^^^^^^
</hint>

If that doesn't work, try <ctype.h> .

Also, read the documentation carefully: those functions accept
a value in the range [0, UCHAR_MAX]; if you have some plain
or signed chars then you have to cast them to unsigned char
before passing them.

<excuse>
It was late and my mind was elsewhere.
</excuse>

I'll do better next time.
 
D

Default User

QQ said:
for instance, I read a char from the input
and I need to decide whether it is a letter or a number

Pick a language and newsgroup. The same question was multi-posted to
comp.lang.c.



Brian
 
D

Default User

Default said:
Pick a language and newsgroup. The same question was multi-posted to
comp.lang.c.

Scratch this. When I saw the same question again, I thought it was in a
different newsgroup. I'll attempt a cancel.




Brian
 
S

Simon Biber

QQ said:
for instance, I read a char from the input
and I need to decide whether it is a letter or a number
What I am doing is
char a;
...... // read a
int true = false;

That's terribly confusing. You have defined false previously, hopefully
as 0, and are now defining a new variable 'true' and giving it the value
of 'false'!

It's poor style to define your own variables with names 'true' and
'false'. They are defined by the said:
if(( (a >='0') && (a <='9')) | | ((a >='a') && (a <= 'z')) ||((a >='A')
&& (a <= 'Z')))
true = 1;

Because digits are guaranteed to be consecutive and in order from 0 to
9, you can determine digit by (a >= '0') && (a <= '9').

However, letters are not guaranteed to be consecutive and in order. You
should never check for letters by (a >= 'a') && (a <= 'z').
Is there any easier way for it?

Absolutely!

#include <ctype.h>

char a;
// read a
if(isdigit((unsigned char)a))
{
printf("it's a number\n");
}
else if(isalpha((unsigned char)a))
{
printf("it's a letter\n");
}
else
{
printf("it's neither a number nor a letter\n");
}

Alternatively, if want a boolean value that expresses whether it's
either a number or a letter:

bool number_or_letter = isdigit((unsigned char)a)
|| isalpha((unsigned char)a);

It's important to include the cast to (unsigned char), since the is*
functions expect a non-negative argument in the range 0 .. UCHAR_MAX, or
else EOF.
 
D

Dave Thompson

Because digits are guaranteed to be consecutive and in order from 0 to
9, you can determine digit by (a >= '0') && (a <= '9').
Right.

However, letters are not guaranteed to be consecutive and in order. You
should never check for letters by (a >= 'a') && (a <= 'z').
Not preferred, but I wouldn't go as far as 'never'. There are some
cases where the limited unportability is a reasonable tradeoff.

Alternatively, if want a boolean value that expresses whether it's
either a number or a letter:

bool number_or_letter = isdigit((unsigned char)a)
|| isalpha((unsigned char)a);
Or in this particular case just use (standard) isalnum().
It's important to include the cast to (unsigned char), since the is*
functions expect a non-negative argument in the range 0 .. UCHAR_MAX, or
else EOF.

It is important the argument be in that range. In some cases it
already surely is, as for example it was the value returned from
getchar(). If it isn't, or maybe not, then the cast is needed.

- David.Thompson1 at worldnet.att.net
 
R

Richard Bos

Dave Thompson said:
Not preferred, but I wouldn't go as far as 'never'. There are some
cases where the limited unportability is a reasonable tradeoff.

Such as those cases where you don't have a definition of isalpha()?
AFAIK that's in one of the headers which is required even for
freestanding implementations.

Richard
 
C

CBFalconer

Richard said:
Such as those cases where you don't have a definition of isalpha()?
AFAIK that's in one of the headers which is required even for
freestanding implementations.

Not so. All the absolutely required headers provide definitions
alone.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top