K&R2, page 106 <strcmp>

Discussion in 'C Programming' started by WANG Cong, Apr 10, 2008.

  1. WANG Cong

    WANG Cong Guest

    arnuld wrote:

    > I am suing the strcmp function from K&R2, page 106 which is used here:
    >
    > http://clc-wiki.net/wiki/K&R2_solutions:Chapter_5:Exercise_4
    >
    >
    > I only changed the name to avoid confusion with real strcmp. K&R2 says:
    > it returns < 0, if s <t, 0 if s== t, > 0 if s > t.
    >
    > but I have found, it returns < 0 if s > t and vice-versa.
    >
    >
    > int my_strcmp( char* s, char* t )
    > {
    > for(; *s == *t; ++s, ++t )
    > {
    > printf("*s = %c\n", *s);
    > printf("*t = %c\n", *t);
    >
    > if( *s == '\0' )
    > {
    > return 0;
    > }
    > }
    >
    > return *s - *t;
    > }
    >
    >
    >
    > int main(void)
    > {
    > char s[] = "it is ok";
    > char t[] = "nope";
    >
    > printf("comparing <%s> with <%s> = %d\n", s, t, my_strcmp( s, t ));
    >
    > return 0;
    >
    > }


    's' is definitely less than 't' here.

    --
    Hi, I'm a .signature virus, please copy/paste me to help me spread
    all over the world.
     
    WANG Cong, Apr 10, 2008
    #1
    1. Advertising

  2. WANG Cong

    arnuld Guest

    I am suing the strcmp function from K&R2, page 106 which is used here:

    http://clc-wiki.net/wiki/K&R2_solutions:Chapter_5:Exercise_4


    I only changed the name to avoid confusion with real strcmp. K&R2 says:
    it returns < 0, if s <t, 0 if s== t, > 0 if s > t.

    but I have found, it returns < 0 if s > t and vice-versa.


    int my_strcmp( char* s, char* t )
    {
    for(; *s == *t; ++s, ++t )
    {
    printf("*s = %c\n", *s);
    printf("*t = %c\n", *t);

    if( *s == '\0' )
    {
    return 0;
    }
    }

    return *s - *t;
    }



    int main(void)
    {
    char s[] = "it is ok";
    char t[] = "nope";

    printf("comparing <%s> with <%s> = %d\n", s, t, my_strcmp( s, t ));

    return 0;

    }

    =========== OUTPUT ============
    /home/arnuld/programs/C $ gcc -ansi -pedantic -Wall -Wextra 5-5.c
    /home/arnuld/programs/C $ ./a.out
    comparing <it is ok> with <nope> = -5
    /home/arnuld/programs/C $


    --
    http://lispmachine.wordpress.com/

    Please remove capital 'V's when you reply to me via e-mail.
     
    arnuld, Apr 10, 2008
    #2
    1. Advertising

  3. WANG Cong

    Bartc Guest

    "arnuld" <> wrote in message
    news:p...
    >I am suing the strcmp function from K&R2, page 106 which is used here:


    I wouldn't go that far :)

    >
    > http://clc-wiki.net/wiki/K&R2_solutions:Chapter_5:Exercise_4
    >
    >
    > I only changed the name to avoid confusion with real strcmp. K&R2 says:
    > it returns < 0, if s <t, 0 if s== t, > 0 if s > t.
    >
    > but I have found, it returns < 0 if s > t and vice-versa.


    Perhaps relationship tests on strings mean something different from what you
    think. strcmp() does not compare lengths, only alphabetic ordering. So "z" >
    "aaaaaaaaaaaa".

    --
    Bart
     
    Bartc, Apr 10, 2008
    #3
    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. Mo
    Replies:
    5
    Views:
    362
    Knute Johnson
    Feb 16, 2007
  2. Replies:
    1
    Views:
    1,859
    Dennis Lee Bieber
    Jan 18, 2007
  3. Ruby Quiz

    [QUIZ] Chess960 (#106)

    Ruby Quiz, Dec 15, 2006, in forum: Ruby
    Replies:
    20
    Views:
    275
    James Edward Gray II
    Dec 21, 2006
  4. Ruby Quiz

    [SUMMARY] Chess960 (#106)

    Ruby Quiz, Dec 21, 2006, in forum: Ruby
    Replies:
    6
    Views:
    105
  5. Alex Watt

    [RUBY QUIZ #106], too late?

    Alex Watt, Dec 30, 2006, in forum: Ruby
    Replies:
    1
    Views:
    96
    James Edward Gray II
    Dec 31, 2006
Loading...

Share This Page