CompareWithoutRegardToCase

J

JKop

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;
}
 
B

Buster

JKop said:
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));
}
 
B

Buster

Buster said:
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.

 
J

JKop

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
 
J

JKop

JKop posted:
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
 
B

Buster

JKop said:
JKop posted:

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

Siemel Naran

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top