Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > How to get strcmp() to work with number strings?

Reply
Thread Tools

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

 
 
Steve555
Guest
Posts: n/a
 
      01-07-2009
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
 
Reply With Quote
 
 
 
 
David Resnick
Guest
Posts: n/a
 
      01-07-2009
On Jan 7, 10:24*am, Steve555 <(E-Mail Removed)> wrote:
> 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
 
Reply With Quote
 
 
 
 
Willem
Guest
Posts: n/a
 
      01-07-2009
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
 
Reply With Quote
 
jameskuyper
Guest
Posts: n/a
 
      01-07-2009


Steve555 wrote:
> 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.
 
Reply With Quote
 
Steve555
Guest
Posts: n/a
 
      01-07-2009
On 7 Jan, 16:29, jameskuyper <(E-Mail Removed)> wrote:
> Steve555 wrote:
> > 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.


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


 
Reply With Quote
 
jameskuyper
Guest
Posts: n/a
 
      01-07-2009
Steve555 wrote:
> On 7 Jan, 16:29, jameskuyper <(E-Mail Removed)> wrote:

....
> >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.)


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.
 
Reply With Quote
 
Steve555
Guest
Posts: n/a
 
      01-07-2009
On 7 Jan, 22:09, jameskuyper <(E-Mail Removed)> wrote:
> Steve555 wrote:
> > On 7 Jan, 16:29, jameskuyper <(E-Mail Removed)> wrote:

> ...
> > >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.)

>
> 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
 
Reply With Quote
 
MisterE
Guest
Posts: n/a
 
      01-08-2009

"Steve555" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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.


 
Reply With Quote
 
MisterE
Guest
Posts: n/a
 
      01-08-2009

> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
number of processors, number of cores, how can I get these numbers? Jekyll C++ 2 04-24-2007 03:58 PM
OT: Number Nine, Number Nine, Number Nine FrisbeeŽ MCSE 37 09-26-2005 04:06 PM
Re: PIX-FLASH-16MB - can't get it to work : Serial number: 0 (0x0) Nolajc2000 Cisco 2 04-01-2004 06:40 AM



Advertisments