C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CompareWithoutRegardToCase

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
JKop
Guest





PostPosted: Mon Sep 27, 2004 10:55 am    Post subject: CompareWithoutRegardToCase Reply with quote



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;
}
Back to top
Buster
Guest





PostPosted: Mon Sep 27, 2004 3:51 pm    Post subject: Re: CompareWithoutRegardToCase Reply with quote



JKop wrote:
Quote:
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

Back to top
Buster
Guest





PostPosted: Mon Sep 27, 2004 3:54 pm    Post subject: Re: CompareWithoutRegardToCase Reply with quote



Buster wrote:

Quote:
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.


Quote:
--
Regards,
Buster

Back to top
JKop
Guest





PostPosted: Mon Sep 27, 2004 5:48 pm    Post subject: Re: CompareWithoutRegardToCase Reply with quote

Quote:
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 '', and return
false.


Your point?


-JKop

Back to top
JKop
Guest





PostPosted: Mon Sep 27, 2004 5:49 pm    Post subject: Re: CompareWithoutRegardToCase Reply with quote

JKop posted:

Quote:
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 '', and return
false.


Your point?


-JKop


Opps, got mixed up there...

The loop detects when it hits ''.

-JKop

Back to top
Buster
Guest





PostPosted: Mon Sep 27, 2004 7:00 pm    Post subject: Re: CompareWithoutRegardToCase Reply with quote

JKop wrote:
Quote:
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 '', and return
false.


Your point?

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

Quote:
Opps, got mixed up there...

The loop detects when it hits ''.

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.

Back to top
Siemel Naran
Guest





PostPosted: Wed Sep 29, 2004 6:03 am    Post subject: Re: CompareWithoutRegardToCase Reply with quote

"JKop" <NULL (AT) NULL (DOT) NULL> wrote


Quote:
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.



Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.