473,407 Members | 2,676 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

Easier way to determine if a string is an alphanumeric number?

Does anyone know if a easier way (built in function, or something) that
can verify that a string is an alphanumeric number? Here is what I am
doing now:

for(i=0; i < strlen(temp); i++){
if(!isalnum(temp[i])){
return 1
}
return 0;

Thanks

Nov 14 '05 #1
9 16978
fooboo wrote:
Does anyone know if a easier way (built in function, or something) that
can verify that a string is an alphanumeric number? Here is what I am
doing now:

for(i=0; i < strlen(temp); i++){
if(!isalnum(temp[i])){
return 1
}
return 0;

Thanks


What is an alphanumeric number? isalnum means is a digit or is a
letter.
Do you mean isdigit or isxdigit?

Anyway, that way seems fine -- the code is clear and simple.
Why do you want an easier way? Taking strlen of temp in
the loop guard risks bad performance. The compiler COULD
know that isalnum won't change temp and optimize it out,
but it could also compute the length of temp every time
through the loop.

If you want a different way, here are some options:
1) If it won't be a ton of digits, use strtol (or strtoll).
Those have slightly different semantics than you
describe, in that they will eat leading whitespace
and accept +/- signs. Perhaps you don't want to
accept those, but you should be at least aware of
the issues.

2) Or you could do this:
if (strspn(temp, "0123456789") == strlen(temp)) {
return 0;
}
else {
return 1;
}

-David

Nov 14 '05 #2
"fooboo" <jo*********@gmail.com> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com...
Does anyone know if a easier way (built in function, or something) that
can verify that a string is an alphanumeric number? Here is what I am
doing now:

for(i=0; i < strlen(temp); i++){
if(!isalnum(temp[i])){
return 1
}
return 0;


Making an assumption (based on returns) that you're writing a test
function...

int
test_isalnum(char *s)
{
while(isalnum(*s))
++s;
return *s == 0;
}

That should do the trick ;)
Mark
Nov 14 '05 #3

"Mark" <so***@localbar.com> wrote in message
news:QC********************@monger.newsread.com...
"fooboo" <jo*********@gmail.com> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com...
Does anyone know if a easier way (built in function, or something) that
can verify that a string is an alphanumeric number? Here is what I am
doing now:

for(i=0; i < strlen(temp); i++){
if(!isalnum(temp[i])){
return 1
}
return 0;


Making an assumption (based on returns) that you're writing a test
function...

int
test_isalnum(char *s)
{
while(isalnum(*s))
++s;
return *s == 0;
}

That should do the trick ;)
Mark


Albeit my return value is opposite that of the original code...
if you want to return the opposite, change the return statement to:
return *s != 0;
though my preference would be to see it return true appropriately!

Mark
Nov 14 '05 #4
>What is an alphanumeric number? isalnum means is a digit or is a
letter.
Do you mean isdigit or isxdigit?


your right, I mixed it up. isdigit() is what I should have used. My
program worked just by coincidence.

I was just wondering if there was I library function I was not aware of.

Nov 14 '05 #5


fooboo wrote:
What is an alphanumeric number? isalnum means is a digit or is a
letter.
Do you mean isdigit or isxdigit?



your right, I mixed it up. isdigit() is what I should have used. My
program worked just by coincidence.

I was just wondering if there was I library function I was not aware of.


What will be your next step after discovering that the
string consists entirely of digits? If the next step is
"convert the string to its numeric equivalent," you could
just eliminate the test altogether, attempt the conversion
with strtol() or strtoul() (or even strtod(), if you want
to handle floating-point numbers), and then check whether
the conversion succeeded.

Note that the check for success is necessary anyhow;
the string "999999999999999999999999999999999999999999999 "
contains only digits, but probably doesn't represent a valid
`long' value.

By the way, when you use isdigit() or any of the other
<ctype.h> functions on characters plucked from a string, be
sure to use this (somewhat non-intuitive) idiom:

if (isdigit( (unsigned char) temp[i] )) ...

The `char' type can be signed, so a particular `char' value
might be negative -- but the only negative value acceptable
to isxxx() and toxxx() is EOF. Programmers who fail to heed
this advice write programs that work in the USA but suddenly
fail when taken to München or St Estèphe.

--
Er*********@sun.com

Nov 14 '05 #6
>What will be your next step after discovering that the
string consists entirely of digits? The design spec requires that it be stored as a string, even though it
should always be a number, don't ask me why. Note that the check for success is necessary anyhow;
the string "999999999999999999999999999999999999999999999 "
contains only digits, but probably doesn't represent a valid
`long' value. the program flow won't allow the string I'm testing to be anything
other than a string of length 5 anyway, so a long data type would work,
if it wasn't for the design spec.By the way, when you use isdigit() or any of the other
<ctype.h> functions on characters plucked from a string, be
sure to use this (somewhat non-intuitive) idiom:

good tip, that would have never even occured to me :)

Nov 14 '05 #7
"Eric Sosman" <er*********@sun.com> wrote in message
news:d8**********@news1brm.Central.Sun.COM...
[snip] What will be your next step after discovering that the
string consists entirely of digits? If the next step is
"convert the string to its numeric equivalent," you could
just eliminate the test altogether, attempt the conversion
with strtol() or strtoul() (or even strtod(), if you want
to handle floating-point numbers), and then check whether
the conversion succeeded.


According to the man pages on my machine if strtod (for example) fails errno
MAY be set to something, but it doesn't seem to be required : "If no
conversion could be performed, 0 is returned and errno may be set to
EINVAL." So how can you verify that it succeeded? Is this specified in the
Standard (I don't have a copy handy), or it implementation specific and thus
non-portable?

Thanx,
Charles
Nov 14 '05 #8
>According to the man pages on my machine if strtod (for example) fails errno
MAY be set to something, but it doesn't seem to be required : "If no
conversion could be performed, 0 is returned and errno may be set to
EINVAL." So how can you verify that it succeeded? Is this specified in the
Standard (I don't have a copy handy), or it implementation specific and thus
non-portable?


If you really want to know:
http://groups-beta.google.com/group/...67df53c744f6ed

Nov 14 '05 #9


Charles M. Reinke wrote:
"Eric Sosman" <er*********@sun.com> wrote in message
news:d8**********@news1brm.Central.Sun.COM...

[snip]
What will be your next step after discovering that the
string consists entirely of digits? If the next step is
"convert the string to its numeric equivalent," you could
just eliminate the test altogether, attempt the conversion
with strtol() or strtoul() (or even strtod(), if you want
to handle floating-point numbers), and then check whether
the conversion succeeded.

According to the man pages on my machine if strtod (for example) fails errno
MAY be set to something, but it doesn't seem to be required : "If no
conversion could be performed, 0 is returned and errno may be set to
EINVAL." So how can you verify that it succeeded? Is this specified in the
Standard (I don't have a copy handy), or it implementation specific and thus
non-portable?


You've got to look at two different kinds of failure: input
that doesn't have numeric form, and input that has correct form
but is out of range. To detect the first kind, use the second
argument to these functions, which (if non-NULL) designates a
`char*' variable that will receive a pointer to the first input
character that was not converted -- if no conversion could be
performed at all, this will be a pointer to the start of the
input string. If the input has the correct form but the result
is out of range, the function always sets errno to ERANGE. So
the whole dance goes something like this:

char *input = ...;
char *end;
double value;

errno = 0; /* in case it was ERANGE already */
value = strtod(input, &end);
if (end == input)
not_convertible();
else if (errno == ERANGE)
out_of_range();
else
successful_conversion();

If you want to require that the input consist only of a number
(not a number followed by other stuff), you could add a further
test for *end == '\0' -- that's up to you.

Sounds like a lot of tests, but in practice one usually lumps
all the error cases together, to get something like

errno = 0;
value = strtod(input, &end);
if (end == input || *end != '\0' || errno == ERANGE)
some_kind_of_error();
else
successful_conversion();

.... which isn't really all that daunting.

There are certainly more direct ways to answer exactly the
question the O.P. asked: "Does this string consist entirely of
digits?" The only reason I mentioned strtoxxx() is because the
next step after discovering that a string is composed of digits
is often to try to extract the value they represent; since
strtoxxx() will do the necessary checking anyhow, it may be
simpler just to attempt the conversion and see what happens.

--
Er*********@sun.com

Nov 14 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Kenneth McDonald | last post by:
I'm working on the 0.8 release of my 'rex' module, and would appreciate feedback, suggestions, and criticism as I work towards finalizing the API and feature sets. rex is a module intended to make...
10
by: M Bourgon | last post by:
I'm trying to figure out how to find the last whitespace character in a varchar string. To complicate things, it's not just spaces that I'm looking for, but certain ascii characters (otherwise,...
3
by: trint | last post by:
How can I check this to see if it is a string representation of a number, or if it is actually just text? Convert.Int32(AlphaNumeric); returns the error for wrong format if it's alpha. I just...
10
by: Crirus | last post by:
Is there a function that return some random ID like string alphanumeric? Like this: A35sDsd1dSGsH Thanks Crirus
7
by: Fernando Rodríguez | last post by:
Hi, How can I know if a string only has alfanumeric chars? Thanks
25
by: lovecreatesbeauty | last post by:
Hello experts, I write a function named palindrome to determine if a character string is palindromic, and test it with some example strings. Is it suitable to add it to a company/project library...
10
by: micklee74 | last post by:
hi if i have a some lines like this a ) "here is first string" b ) "here is string2" c ) "here is string3" When i specify i only want to print the lines that contains "string" ie the first...
6
by: James Arnold | last post by:
Hello, I am new to C and I am trying to write a few small applications to get some hands-on practise! I am trying to write a random string generator, based on a masked input. For example, given...
1
by: shalabh6 | last post by:
Hi, A js file containing a function which is passing an alphanumeric string to another function in the same file, the second funtion requires 4 parameters to pass from the first function. The...
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
marktang
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,...
0
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...
0
Oralloy
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,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.