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! 10 1960
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.
"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
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.
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.
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
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
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.
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
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Ricky Romaya |
last post by:
Hi,
Anybody could show me a regex for capturing words (alphas, without
numerics) in languages other than english (languages with special
characters i.e. french, german)? I've tried '+' but the...
|
by: smjmitchell |
last post by:
Hi,
I am writing an application in VB6.0 that will have the option to
select the language. For instance when Spanish is selected all the
text on the program interface will display in Spanish.
...
|
by: usgog |
last post by:
I need to implement a function to return True/false whether String A
contains String B. For example, String A = "This is a test"; String B =
"is is". So it will return TRUE if String A includes two...
|
by: OrenFlekser |
last post by:
Hi
I've posted this message couple of days ago, but I can't find it now,
so sorry if you see it twice...
Anyways - I have a text box, and I want my users to be able to write
only in english...
|
by: Gidi |
last post by:
Hi,
For the last week, i'm looking for a way to make a TextBox always write in
English (No matter what the OS default language is).
i asked here few times but the answers i got didn't help me. i...
|
by: darrel |
last post by:
I can grab a random number in vb.net like this:
Dim RandomClass As New Random
Dim RandomNumber As Integer
RandomNumber = RandomClass.Next(1, 26)
However, what I want is a random number. Short...
|
by: jerger |
last post by:
I want to help teach to a minority group in Milwaukee, so I want to create a dictionary program that translates a sentence (like a homework problem or teacher instructions), from English into Hmong....
|
by: Ulterior |
last post by:
Hi, everyone,
I have a simple problem, which bothers me for some while. I will try
to explain it -
There is some string, whith different letters in it. Is it possible to
analyse this string...
|
by: socondc22 |
last post by:
Im stuck at this point. The way i was trying to do this was running if statement around my void function and then a else if statement around a different void function but it didnt work... right now...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |