469,963 Members | 1,820 Online

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

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!

Jun 7 '06 #1
10 1779
QQ wrote:
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>
--
Remove "your coat" to tontact me.
Jun 7 '06 #2

"QQ" <ju****@yahoo.com> wrote in message
news:11**********************@f6g2000cwb.googlegro ups.com...
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.
--
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Technical Architect, Software Reuse Project
Jun 8 '06 #3
Dale wrote:
QQ wrote:
for instance, I read a char from the input
and I need to decide whether it is a letter or a number

<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.

Jun 8 '06 #4
Old Wolf wrote:
Dale wrote:
QQ wrote:
for instance, I read a char from the input
and I need to decide whether it is a letter or a number

<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.

--
Remove "your coat" to tontact me.
Jun 8 '06 #5
QQ wrote:
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
Jun 8 '06 #6
Default User wrote:
QQ wrote:
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.

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

Brian
Jun 8 '06 #7
QQ wrote:
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 <stdbool.h> header in C99.
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.

--
Simon.
Jun 9 '06 #8
On Fri, 09 Jun 2006 05:53:24 GMT, Simon Biber <ne**@ralmin.cc> wrote:
<snip>
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.

<snip> 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
Jun 19 '06 #9
Dave Thompson <da*************@worldnet.att.net> wrote:
On Fri, 09 Jun 2006 05:53:24 GMT, Simon Biber <ne**@ralmin.cc> wrote:
<snip>
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.

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
Jun 19 '06 #10
Richard Bos wrote:
Dave Thompson <da*************@worldnet.att.net> wrote:
Simon Biber <ne**@ralmin.cc> wrote:
<snip>
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.

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.

--
"A man who is right every time is not likely to do very much."
-- Francis Crick, co-discover of DNA
"There is nothing more amazing than stupidity in action."
-- Thomas Matthews

Jun 19 '06 #11

### This discussion thread is closed

Replies have been disabled for this discussion.

### Similar topics

 4 posts views Thread by Ricky Romaya | last post: by 3 posts views Thread by smjmitchell | last post: by 13 posts views Thread by usgog | last post: by 18 posts views Thread by OrenFlekser | last post: by 14 posts views Thread by Gidi | last post: by 4 posts views Thread by darrel | last post: by 91 posts views Thread by jerger | last post: by 9 posts views Thread by Ulterior | last post: by 3 posts views Thread by socondc22 | last post: by reply views Thread by eddparker01 | last post: by reply views Thread by eddparker01 | last post: by reply views Thread by isladogs | last post: by 1 post views Thread by isladogs | last post: by reply views Thread by Trystan | last post: by reply views Thread by Trystan | last post: by 2 posts views Thread by elpidahope | last post: by 1 post views Thread by helenbrown | last post: by 1 post views Thread by rainxy | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.