How to get strcmp() to work with number strings?

S

Steve555

Hi

I'm comparing two strings with strcmp(), but they might be words or
numbers. Is there a convenient way to handle number strings with strcmp
() such that 4 is less than 2300 for example, or do I have to manually
check if they're numbers and handle this case separately each time?

Thanks

Steve
 
D

David Resnick

Hi

I'm comparing two strings with strcmp(), but they might be words or
numbers. Is there a convenient way to handle number strings with strcmp
() such that 4 is less than 2300 for example, or do I have to manually
check if they're numbers and handle this case separately each time?

Thanks

Steve

strcmp only compares them as strings, a character at a time, until the
first different character is reached or both strings reach the NUL.
Since 4 and 2 are different, the comparison would stop there with the
character '4' being after the character '2'.

So you need to use strtol or some such, and if both are numbers handle
them separately. Or have the data tagged in some way (e.g. in a
struct) with the type of string noted so you know without looking how
to compare them.

-David
 
W

Willem

Steve555 wrote:
) I'm comparing two strings with strcmp(), but they might be words or
) numbers. Is there a convenient way to handle number strings with strcmp
) () such that 4 is less than 2300 for example, or do I have to manually
) check if they're numbers and handle this case separately each time?

Not with stcmp(), no.

But there are some people who wrote compare functions that are
number-savvy. Probably some freeware versions as well. GIYF.


SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
 
J

jameskuyper

Steve555 said:
Hi

I'm comparing two strings with strcmp(), but they might be words or
numbers. Is there a convenient way to handle number strings with strcmp
() such that 4 is less than 2300 for example, or do I have to manually
check if they're numbers and handle this case separately each time?

If you want them sorted according to their numerical value, rather
than as strings, then you have to calculate that numerical value. That
means you have to actually determine whether they are numbers.

Do you have any control over the content of those number strings? If
all of your number strings represent positive integers, and if you can
pad the strings with leading '0' characters to all be the same length
(for instance, by using printf("%09u" ...) ), then strcmp() will order
them the same way as their numerical value.

You can also pad with blanks instead of '0' by using the '-' flag
rather than the '0' flag, but this only works if ' ' < '0'. That
happens to be true in every encoding I'm familiar with, but it's not
required by the C standard.

Do the strings sometimes have a mixture of numbers and letters? If so,
you'll have to decide how you want "4C" to sort relative to "20".
There's lots of possible ways of answering that question, and the
appropriate method of dealing with them depends upon the answer.
 
S

Steve555

If you want them sorted according to their numerical value, rather
than as strings, then you have to calculate that numerical value. That
means you have to actually determine whether they are numbers.

Do you have any control over the content of those number strings? If
all of your number strings represent positive integers, and if you can
pad the strings with leading '0' characters to all be the same length
(for instance, by using printf("%09u" ...) ), then strcmp() will order
them the same way as their numerical value.

You can also pad with blanks instead of '0' by using the '-' flag
rather than the '0' flag, but this only works if ' ' < '0'. That
happens to be true in every encoding I'm familiar with, but it's not
required by the C standard.

Do the strings sometimes have a mixture of numbers and letters? If so,
you'll have to decide how you want "4C" to sort relative to "20".
There's lots of possible ways of answering that question, and the
appropriate method of dealing with them depends upon the answer.

Thanks for all the advice, strnatcmp() will do the job nicely.
Do you have any control over the content of those number strings? If
all of your number strings represent positive integers, and if you can
pad the strings with leading '0' characters to all be the same length
(for instance, by using printf("%09u" ...) ), then strcmp() will order
them the same way as their numerical value.

Yes, they're always positive integers, but how will printf("%09u" ...)
help, as they are already strings?
Did you mean convert them to longs, then back to strings? I see how
padding would help sort them, but using printf("%09u" ...) on strings
produces gibberish. (Sorry, I guess I've misunderstood you.)

Thanks

Steve
 
J

jameskuyper

Steve555 said:
Yes, they're always positive integers, but how will printf("%09u" ...)
help, as they are already strings?
Did you mean convert them to longs, then back to strings? I see how
padding would help sort them, but using printf("%09u" ...) on strings
produces gibberish. (Sorry, I guess I've misunderstood you.)

No, I'm not suggesting that you change the strings within your
program. I'm asking about whether you have any ability to change the
contents of those strings before they reach your program. You've told
us nothing about how those strings were created, but if the number
strings were created by a C program using printf("%u", ...), then
changing the format code in THAT program to "%09u" would solve your
problem. This suggestion is absolutely useless if you have no power to
change the format of the input strings.
 
S

Steve555

No, I'm not suggesting that you change the strings within your
program. I'm asking about whether you have any ability to change the
contents of those strings before they reach your program. You've told
us nothing about how those strings were created, but if the number
strings were created by a C program using printf("%u", ...), then
changing the format code in THAT program to "%09u" would solve your
problem. This suggestion is absolutely useless if you have no power to
change the format of the input strings.

Apologies, misunderstood. No they're being read from disk and I can't
control them.

Steve
 
M

MisterE

Steve555 said:
Hi

I'm comparing two strings with strcmp(), but they might be words or
numbers. Is there a convenient way to handle number strings with strcmp
() such that 4 is less than 2300 for example, or do I have to manually
check if they're numbers and handle this case separately each time?

You could use sprintf to print the number to an actual integer, just check
that each character is [0..9] first to avoid doing it for wods.
 
M

MisterE

You could use sprintf to print the number to an actual integer, just check
that each character is [0..9] first to avoid doing it for wods.

oops i meant sscanf
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top