how do I tell whether the read 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 <='z')
true = 1;


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

Dann Corbit

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 <='z')
true = 1;


Is there any easy way for it?

untested junk follows:

#include <ctype.h>
#include <stdio.h>
#include <string.h>
....
char string[32767];
if (fgets(string, sizeof string, stdin))
{
int i;
int ival;
for (i = 0; i < strlen(string); i++)
ival = string;
if (isalpha(ival)) puts("letter");
if (isdigit(ival)) puts("digit");
if (ispunct(ival)) puts("punctuation");
}
}
 
S

spibou

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 <='z')
true = 1;

This is not portable since it depends on the way your platform codes
characters
and digits. It won't work with ASCII encoding.
Is there any easy way for it?

Yes and it's called isalnum()

Spiros Bousbouras
 
I

Ian Collins

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

Have a look at the is.... macros form the standard library, isalnum(),
probably does what you want.
What I am doing is
char a;
...... // read a
int true = false;

Not a good choice of variable name.
 
S

spibou

Dann said:
How to decide whether it is an English letter or a number?
char a;
..... // read a
int true = false;
if(( (a >='0') && (a <='9')) | | ((a >='a') && (a <= 'z')) ||((a >='A')
&& (a <= 'Z')))
true = 1;

Once again this depends on the character encoding. There's no
guarantee that everything >= 'a' and <= 'z' will be a letter.

I also notice that you ask for *English* letter. I don't know
if the value of the locale affects the behaviour of isalnum()
but I suspect that it does. You would need to use setlocale()
to select the appropriate locale for the English language.
 
S

spibou

pete said:
What are you quoting?
I can't find that text and I can't find that code
anywhere else in this thread,

I am quoting the first message of the thread titled "How to decide
whether it
is an English letter or a number?". I figured since both threads
discuss the same
topic it would be best to have all the comments together in one.
 
P

pete

I am quoting the first message of the thread titled
"How to decide whether it
is an English letter or a number?".

No, you aren't quoting anything that anybody ever wrote.

This is from that thread:

"for instance, I read a char from the input
and I need to decide whether it is a letter or a number"
I figured since both threads
discuss the same
topic it would be best to have all the comments together in one.

Then you should quote the thread that you are replying to,
and let the other thread die.
 
S

spibou

pete said:
No, you aren't quoting anything that anybody ever wrote.

This is from that thread:

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

And below that is the code I quoted. I also quoted the title of the
thread.
Then you should quote the thread that you are replying to,
and let the other thread die.

I did quote the thread I was replying to. Or do you mean I should
(only) quote the thread *in which* I'm replying to ? Why should I
restrict
myself like this ? Although I guess I should have mentioned that I
was quoting another thread to avoid confusion.

I felt that the opening post of the other thread would benefit from a
comment so I wasn't willing to just let it die.
 
P

pete

Or do you mean I should
(only) quote the thread *in which* I'm replying to ? Why should I
restrict myself like this ?

I'll post the answer to that question, in another thread.

BTW, you didn't quote any of what Dann Corbit actually posted.
 
S

spibou

pete said:
BTW, you didn't quote any of what Dann Corbit actually posted.

You are right , I was quoting QQ and for some reason I thought that
Dann Corbit had written the message.
 
C

CBFalconer

Once again this depends on the character encoding. There's no
guarantee that everything >= 'a' and <= 'z' will be a letter.

Dan Corbit wrote nothing of the sort and knows much better than to
write such junk. Try to take some elementary care with your
quotations.

--
"Our enemies are innovative and resourceful, and so are we.
They never stop thinking about new ways to harm our country
and our people, and neither do we." -- G. W. Bush.
"The people can always be brought to the bidding of the
leaders. All you have to do is tell them they are being
attacked and denounce the pacifists for lack of patriotism
and exposing the country to danger. It works the same way
in any country." --Hermann Goering.
 
D

Default User

You are right , I was quoting QQ and for some reason I thought that
Dann Corbit had written the message.

Why aren't you using the real quote mechanism from Google instead of
cutting and pasting?



Brian
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top