Re: CompareWithoutRegardToCase

From: Buster (none_at_none.com)
Date: 09/27/04


Date: Mon, 27 Sep 2004 16:51:10 +0100

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


Relevant Pages

  • Re: Function Problem
    ... > but fnReplace takes pointers to strings of length 16. ... > void fnReplace (char *RplStr) ...
    (microsoft.public.windowsce.embedded.vc)
  • Re: palindrome iteration
    ... empty strings, for input strings that contain many words with mixed ... So we use iterative loops and single char tests. ... To avoid bugs like ones in your code you may think about the loop ... 'int' object has no attribute 'lower' ...
    (comp.lang.python)
  • Re: initializing table of strings
    ... >sense to set strings in decleration. ... The fact that it is an array of pointers to const char does not matter ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Sets and portability (was) Re: Is ISO Pascal compatible with J&W (original) Pascal ?
    ... strings, the user can control the length by the data they process; ... >> The computer world is more complex than it's ever been (eg Unicode) ... The Pascal `Char' type can be this size (unlike C, ... > Note that ansi->wide conversion is codepage sensitive. ...
    (comp.lang.pascal.misc)
  • Re: socket communication: send & receive doesnt work right
    ... But can I actually send and receive strings? ... I did a test of sending two doubles: ... public void send_doubles(double vals, int len) throws ... char *result; ...
    (microsoft.public.win32.programmer.networks)