473,394 Members | 1,806 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

CompareWithoutRegardToCase

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;
}
Jul 22 '05 #1
6 1036
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
Jul 22 '05 #2
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

Jul 22 '05 #3
> 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
Jul 22 '05 #4
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
Jul 22 '05 #5
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.
Jul 22 '05 #6
"JKop" <NU**@NULL.NULL> wrote in message
news:8k*******************@news.indigo.ie...
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.
Jul 22 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.