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

Discussion in 'C Programming' started by Steve555, Jan 7, 2009.

  1. Steve555

    Steve555 Guest

    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
    Steve555, Jan 7, 2009
    #1
    1. Advertising

  2. On Jan 7, 10:24 am, 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?
    >
    > 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
    David Resnick, Jan 7, 2009
    #2
    1. Advertising

  3. Steve555

    Willem Guest

    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
    Willem, Jan 7, 2009
    #3
  4. Steve555

    jameskuyper Guest

    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.
    jameskuyper, Jan 7, 2009
    #4
  5. Steve555

    Steve555 Guest

    On 7 Jan, 16:29, jameskuyper <> 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
    Steve555, Jan 7, 2009
    #5
  6. Steve555

    jameskuyper Guest

    Steve555 wrote:
    > On 7 Jan, 16:29, jameskuyper <> 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.
    jameskuyper, Jan 7, 2009
    #6
  7. Steve555

    Steve555 Guest

    On 7 Jan, 22:09, jameskuyper <> wrote:
    > Steve555 wrote:
    > > On 7 Jan, 16:29, jameskuyper <> 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
    Steve555, Jan 7, 2009
    #7
  8. Steve555

    MisterE Guest

    "Steve555" <> wrote in message
    news:...
    > 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.
    MisterE, Jan 8, 2009
    #8
  9. Steve555

    MisterE Guest


    > 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
    MisterE, Jan 8, 2009
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Shane Peck

    strcmp problem

    Shane Peck, Sep 19, 2003, in forum: C++
    Replies:
    6
    Views:
    2,351
    Kevin Goodsell
    Sep 22, 2003
  2. muser

    strcmp

    muser, Oct 3, 2003, in forum: C++
    Replies:
    6
    Views:
    1,111
    Frank Schmitt
    Oct 9, 2003
  3. Steven
    Replies:
    9
    Views:
    401
    Keith Thompson
    Dec 29, 2005
  4. Ben

    Strings, Strings and Damned Strings

    Ben, Jun 22, 2006, in forum: C Programming
    Replies:
    14
    Views:
    754
    Malcolm
    Jun 24, 2006
  5. Raman

    do strcmp support wildcard strings

    Raman, Aug 14, 2007, in forum: C Programming
    Replies:
    4
    Views:
    2,914
    Mark Bluemel
    Aug 15, 2007
Loading...

Share This Page