CompareWithoutRegardToCase

Discussion in 'C++' started by JKop, Sep 27, 2004.

  1. JKop

    JKop Guest

    Comments, Questions and Suggestions please!


    Returns true if strings are identical, returns false if strings are not
    identical. (without regard to case).


    bool CompareWithoutRegardToCase(const char* x, const char* y)
    {
    //Undefined Behaviour if supplied with null pointers ;-D

    if ( !*x || !*y ) //valid pointer to null string
    {
    if ( !*x && !*y ) return true; //2 null strings are equal.

    return false;
    }

    do
    {
    if ( tolower( static_cast<unsigned char>(*x) ) == tolower(
    static_cast<unsigned char>(*y) ) ) continue;
    else
    {
    return false;
    }
    }
    while ( (++x,++y) ,*x );

    return !*y;
    }
     
    JKop, Sep 27, 2004
    #1
    1. Advertising

  2. JKop

    Buster Guest

    JKop wrote:
    > Comments, Questions and Suggestions please!
    >
    >
    > Returns true if strings are identical, returns false if strings are not
    > identical. (without regard to case).
    >
    >
    > bool CompareWithoutRegardToCase(const char* x, const char* y)
    > {
    > //Undefined Behaviour if supplied with null pointers ;-D
    >
    > if ( !*x || !*y ) //valid pointer to null string
    > {
    > if ( !*x && !*y ) return true; //2 null strings are equal.
    >
    > return false;
    > }
    >
    > do
    > {
    > if ( tolower( static_cast<unsigned char>(*x) ) == tolower(
    > static_cast<unsigned char>(*y) ) ) continue;
    > else
    > {
    > return false;
    > }
    > }
    > while ( (++x,++y) ,*x );
    >
    > return !*y;
    > }


    What happens if the null-terminated string to which y points is longer
    than the one pointed to by x?

    bool CompareWithoutRegardToCase (char x, char y)
    {
    return std::tolower (static_cast <int> (x))
    == std::tolower (static_cast <int> (y);
    }

    bool CompareWithoutRegardToCase (const char * x, const char * y)
    {
    // Undefined behaviour if supplied with null pointers

    while ((* x) && (* y) && CompareWithoutRegardToCase (* x, * y))
    {
    ++ x;
    ++ y;
    }

    return ! ((* x) || (* y));
    }

    --
    Regards,
    Buster
     
    Buster, Sep 27, 2004
    #2
    1. Advertising

  3. JKop

    Buster Guest

    Buster wrote:

    > JKop wrote:

    ....
    > What happens if the null-terminated string to which y points is longer
    > than the one pointed to by x?


    You get the right answer, that's what. I'd still combine the initial
    test and the do-while into a while loop.


    > --
    > Regards,
    > Buster
     
    Buster, Sep 27, 2004
    #3
  4. JKop

    JKop Guest

    > What happens if the null-terminated string to which y points is longer
    > than the one pointed to by x?


    The loop will detect that x's 'k' is not equal to y's '\0', and return
    false.


    Your point?


    -JKop
     
    JKop, Sep 27, 2004
    #4
  5. JKop

    JKop Guest

    JKop posted:

    >> What happens if the null-terminated string to which y points is longer
    >> than the one pointed to by x?

    >
    > The loop will detect that x's 'k' is not equal to y's '\0', and return
    > false.
    >
    >
    > Your point?
    >
    >
    > -JKop
    >


    Opps, got mixed up there...

    The loop detects when it hits '\0'.

    -JKop
     
    JKop, Sep 27, 2004
    #5
  6. JKop

    Buster Guest

    JKop wrote:
    > JKop posted:
    >
    >
    >>>What happens if the null-terminated string to which y points is longer
    >>>than the one pointed to by x?

    >>
    >>The loop will detect that x's 'k' is not equal to y's '\0', and return
    >>false.
    >>
    >>
    >>Your point?


    Didn't you see my other message, the one where I pointed out my mistake?

    > Opps, got mixed up there...
    >
    > The loop detects when it hits '\0'.


    Not that it matters, but you should say what "it" is.

    The point, anyway, is that you can use the same expression for the 'if'
    statement that you used as the 'while' condition of the do-while loop.
    That being done, you can replace the if statement and the do-while loop
    in your original code with a single while loop.

    --
    Regards,
    Buster.
     
    Buster, Sep 27, 2004
    #6
  7. JKop

    Siemel Naran Guest

    "JKop" <> wrote in message
    news:8kS5d.32147$...

    > Returns true if strings are identical, returns false if strings are not
    > identical. (without regard to case).
    >
    >
    > bool CompareWithoutRegardToCase(const char* x, const char* y)
    > {
    > //Undefined Behaviour if supplied with null pointers ;-D
    >
    > if ( !*x || !*y ) //valid pointer to null string
    > {
    > if ( !*x && !*y ) return true; //2 null strings are equal.
    >
    > return false;
    > }
    >
    > do
    > {
    > if ( tolower( static_cast<unsigned char>(*x) ) == tolower(
    > static_cast<unsigned char>(*y) ) ) continue;
    > else
    > {
    > return false;
    > }
    > }
    > while ( (++x,++y) ,*x );
    >
    > return !*y;
    > }


    Looks good, though the loop and return statement make it look more
    complicated than it really is (my version below looks simpler to me at
    least). Also, I don't think the cast to unsigned char is necessary. In the
    code below, I've also stored the result of *x in a variable of type char so
    as to avoid additional memory lookups due to aliasing -- but I wonder how
    much this improves performance. Your comments are very good too, but I left
    them out to save space.

    bool CompareWithoutRegardToCase(const char* xx, const char* yy) {
    for ( ; ; ++xx, ++yy) {
    char x = tolower(*xx);
    char y = tolower(*yy);
    if (x != y) return false;
    if (!x && !y) return true;
    }
    return false; // never reached
    }

    Be aware that there is std::equal of 5 arguments in #include <algorithm>.

    std::equal(a, a+strlen(a), b, b+strlen(b), eqnocase());

    where

    struct eqnocase {
    bool operator()(char lhs, char rhs) const { return tolower(lhs) ==
    tolower(rhs); }
    };

    The next exercise is to write a CompareWithoutRegardToCase that returns an
    int: < 0 if lhs < rhs, 0 if lhs == rhs, >0 if lhs > rhs. Wait a sec, is
    that the convention of strcmp (I always get confused and have to look it
    up)? Some implementations already provide this function with the name
    stricmp, but it's not part of the standard.
     
    Siemel Naran, Sep 29, 2004
    #7
    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.

Share This Page