470,826 Members | 2,004 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,826 developers. It's quick & easy.

how do I tell whether the read is an English letter or a number

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!

Jun 7 '06 #1
13 1749
"QQ" <ju****@yahoo.com> wrote in message
news:11**********************@y43g2000cwc.googlegr oups.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 <='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[i];
if (isalpha(ival)) puts("letter");
if (isdigit(ival)) puts("digit");
if (ispunct(ival)) puts("punctuation");
}
}
Thanks a lot!

Jun 7 '06 #2

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 <='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

Jun 7 '06 #3
QQ wrote:
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.

--
Ian Collins.
Jun 7 '06 #4
Dann Corbit wrote:
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.

Jun 7 '06 #5
sp****@gmail.com wrote:

Dann Corbit wrote:
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.


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

--
pete
Jun 7 '06 #6

pete wrote:
sp****@gmail.com wrote:

Dann Corbit wrote:
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.


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.

Jun 7 '06 #7
sp****@gmail.com wrote:

pete wrote:
sp****@gmail.com wrote:

Dann Corbit wrote:

> How to decide whether it is an English letter or a number?
What are you quoting?
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.

--
pete
Jun 7 '06 #8

pete wrote:
sp****@gmail.com wrote:

pete wrote:
sp****@gmail.com wrote:
>
> Dann Corbit wrote:
>
> > How to decide whether it is an English letter or a number? What are you quoting?
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"


And below that is the code I quoted. I also quoted the title of the
thread.
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.


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.

Jun 7 '06 #9
sp****@gmail.com wrote:

pete wrote:
sp****@gmail.com wrote:

pete wrote:

> sp****@gmail.com wrote:
> >
> > Dann Corbit wrote:
> >
> > > How to decide whether it is an English letter or a number?
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.

--
pete
Jun 7 '06 #10

pete wrote:
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.

Jun 8 '06 #11
sp****@gmail.com wrote:
Dann Corbit wrote:
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.


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.

Jun 8 '06 #12
sp****@gmail.com wrote:

pete wrote:
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.


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

Brian

--
Please quote enough of the previous message for context. To do so from
Google, click "show options" and use the Reply shown in the expanded
header.
Jun 8 '06 #13

Default User wrote:
sp****@gmail.com wrote:

pete wrote:
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.


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


Because I was quoting from a different thread.

Jun 11 '06 #14

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

303 posts views Thread by mike420 | last post: by
9 posts views Thread by Papiya | last post: by
reply views Thread by mihailmihai484 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.